Categories: PHP

setlocale – PHP String Functions

Syntax :

setlocale ( category, locale );

Description :

The setlocale() function sets the locale information.

Locale information is currency format, currency symbol, language, monetary, time and other information specific for a geographical area.

Note : The setlocale() function changes the locale values only for the current script not for the full website.
Tip: To get numeric formatting information, see the localeconv() function.

Parameter :

  • category – This is Required parameter. It tells which locale information should we set. Category is a named constant specifying the category of the functions affected by the locale setting:
  1. LC_ALL for all of the below
  2. LC_COLLATE for string comparison, see strcoll()
  3. LC_CTYPE for character classification and conversion, for example strtoupper()
  4. LC_MONETARY for localeconv()
  5. LC_NUMERIC for decimal separator (See also localeconv())
  6. LC_TIME for date and time formatting with strftime()
  7. LC_MESSAGES for system responses (available if PHP was compiled with libintl)
  • locale – This is Required parameter. Specifies what country/region to set the locale information to. Can be a string or an array. It is possible to pass multiple locations.
    [table caption=”” width=”100%” colwidth=”20%|80%” colalign=”left|left”]
    Locale value, Result OR Output
    NULL or empty string (“”) , the locale names will be set from the values of environment variables with the same names as the above categories or from “LANG”.
    “0”, the locale setting is not affected just only the current setting is returned.
    Array or followed by some additional parameters,then each array element or parameter is tried to be set as new locale until success. This is useful if a locale is known under different names on different systems or for providing a fallback for a possibly not available locale.
    [/table]

Note : On Windows, setlocale(LC_ALL, ”) sets the locale names from the system’s regional/language settings (accessible via Control Panel).


Output :

Returns the new current locale, or FALSE if the locale functionality is not implemented on your platform, the specified locale does not exist or the category name is invalid.


ChangeLog :

[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, Description
PHP 7.0.0 , Support for the category parameter passed as a string has been removed. Only LC_* constants can be used as of this version.
PHP 5.3.0, This function now throws an E_DEPRECATED notice if a string is passed to the category parameter instead of one o
[/table]


Related articles : localeconv(), fprintf() , number_format()


setlocale() – PHP Functions Example 1 : Set the locale to US English and then back to its original values:
<?php
echo setlocale(LC_ALL,"US");
echo "<br>";
echo setlocale(LC_ALL,NULL);
?>

See below is the output of above code in Web browser.

English_United States.1252
English_United States.1252

jyoti rani

Recent Posts

Modern Toys, Magical Moments: Why the Best Toy Shop in Noida Is More Than Just a Store

When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…

2 months ago

Rediscovering Joy: A New Era of Creativity & Comfort in Toy Stores

In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…

2 months ago

Unboxing Imagination: Discovering the Joy of Play at a Toy Store in Noida

In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…

2 months ago

Imagination Unboxed: Discover Joy at the Toy Shop in Delhi

In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…

2 months ago

Play with Purpose: Discovering the Ultimate Toy Store in Noida

When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…

2 months ago

From Tears to Toys: Exploring Modern Childhood through Delhi’s Favorite Toy Shop

In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…

2 months ago