The PHP setlocale() Function of PHP language is one of the important in-built function and it is helpful in setting the local or locale information. The PHP setlocale() function usually returns the current new locale and if the locale’s functionality is not at all implemented then it is considered as FALSE. Locale/Local information of the setlocale() function of PHP language can be monetary, language, time, or any other info which is very specific for a specific geographical area. The locale can be changed only for the new/current script with the help of setlocale() function. We can also set the locale info to the system default with specific parameters of setlocale() function.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax and parameters of php setlocale() are given below:
Setlocale(constant1, location1);
Constant Parameter of setlocale(): The constant parameter is a mandatory one which is used inside of the setlocale() function of PHP language. It specifies the locale info which is to be set. There are some of the available constant parameter values which are useful a lot in the PHP programming sometimes. They are:
Location Parameter of setlocale(): The location parameter of the setlocale() function of PHP is also an important and mandatory parameter that should be used in setlocale() function. It easily specified what region/country is to be set to the locale information. It can either be an array or a string. It is only possible to pass to multiple locations. If the location is the NULL or empty string (“”) then the location values/names will be set from the environment variable values with the same names as the constants above or from the “LANG”. If the location value is set the value “0” then the location’s setting will not be affected and only the current setting will be returned.
If the location value is an array, the setlocale() function will try each and every array element until it is going to find a valid region code or a valid language. This is very much useful if and only if the region is known under many different systems or names. There are many language codes available for this setlocale() function.
The setlocale() function of the PHP programming language usually works by returning the locale information with the help of the two mandatory parameters. It just returns the locale information/info. The return value of the setlocale() function is the current locale settings but on failure, FALSE will be returned. The value/return value will depend on the PHP System that is actually running. The setlocale() needs PHP 4.0+ versions to produce an output. In PHP 5.3.0 version, if the string content is passed to the specific constant parameter which is instead of the LC_constants, then this function will throw E_DREPRECATED notice.
Following are the examples are given below:
This is the example of implementing the setlocale() function for the location “US”. Here at first PHP tags are created to enter the code which we want to implement. Then hr tag is used after echo statement to print a horizontal line. Then the “location1” variable is created with the string value “USA”. Then location1 variable’s value will be printed with the help of the echo statement. Then the “
” tag is used after echo statement to print a line break. Then setlocale() function of the PHP programming language is used just after the echo statement with the constant and location parameters. So it will print the locale information. Then “
Code:
<?php echo "<hr>"; $location1="USA"; echo "Your Location is:".$location1; echo "<br>"; echo "By using the setlocale() function of PHP :: ".setlocale(LC_ALL,"$location1"); echo "<hr>"; ?>
Output:
This is the example of implementing the setlocale() function of the PHP Programming Language with the NULL value mentioning. Here at first, PHP tags are used to enter the code for the PHP coding language. Then “
Code:
<?php echo "<hr>"; echo "<hr>"; $loc1 ="NULL"; echo "Your Location is: $loc1"; echo "<br>"; echo "By using setlocale() function:".setlocale(LC_ALL,$loc1); echo "<hr>"; echo "<hr>"; ?>
Output:
This is the example of implementing setlocale() function for the location value “US” and “NULL” just one after the other. Here at first, three times “
Code:
<?php echo "<hr>"; echo "<hr>"; echo "<hr>"; echo "This is for the location variable value US :: "; echo setlocale(LC_ALL,"US"); echo "<br>"; echo "At first NULL value produce output as the same previous one <br>"; echo "This is for the location variable value NULL :: "; echo setlocale(LC_ALL,NULL); echo "<hr>"; echo "<hr>"; echo "<hr>"; ?>
Output:
The above is the detailed content of PHP setlocale(). For more information, please follow other related articles on the PHP Chinese website!