Updated April 6, 2023
Definition of PHP setlocale()
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.
Syntax and Parameters
Syntax and parameters of php setlocale() are given below:
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:
- LC_ALL: It means “All the below”
- LC_COLLATE: It means “Sort order”
- LC_CTYPE: It means “Character conversion and Classification” (example: all the characters of it show be upper or lower case)
- LC_MESSAGES: It means “System message Format/Formatting”
- LC_MONETARY: It means “Currency/Monetary Formatting”
- LC_NUMERIC: It means “Number/Numeric Formatting”
- LC_TIME: It means “time and Date formatting”
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.
How setlocale() Function works in PHP?
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.
Examples of PHP setlocale()
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 “<br>” 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 “<hr>” tag is used for the horizontal line just for the view.
<?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>"; ?>
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 “<hr>” tags are used two times to print two horizontal lines. Then “loc1” variable is created with NULL values inside of the inverted commas. Then location variable value will be printed with the help of the echo statement and the “loc1” variable value. Then “<br>” tag is used for the line break purpose just after the echo statement. Then setlocale() PHP function is used just after the echo statement with the two parameters with constant value as LC_ALL and the location variable as NULL. Check the output below once.
<?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>"; ?>
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 “<hr>” tags are used to print horizontal lines 3 times just for view purposes. Then setlocale() function is used with the constant parameter “LC_ALL” and Location parameter value as “US”. Then <br> tag is used just after echo statement to print the line break. Then setlocale() function is used for the Location value “NULL”. Usually, for a NULL value, nothing doesn’t print but here NULL is used just after the usage of “US” in the before setlocale() function. So the output remains the same here just for an instance. Just check out the output so that you will understand.
<?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>"; ?>
This is a guide to PHP setlocale(). Here we also discuss the definition and how setlocale() function work in php along with different examples and its code implementation. You may also have a look at the following articles to learn more –