首页  >  文章  >  后端开发  >  PHP 设置区域设置()

PHP 设置区域设置()

PHPz
PHPz原创
2024-08-29 12:51:02929浏览

PHP 语言的 PHP setlocale() 函数是 PHP 语言中重要的内置函数之一,它有助于设置本地或语言环境信息。 PHP setlocale() 函数通常返回当前的新语言环境,如果语言环境的功能根本没有实现,那么它被认为是 FALSE。 PHP 语言的 setlocale() 函数的区域设置/本地信息可以是货币、语言、时间或任何其他特定于特定地理区域的信息。借助 setlocale() 函数,只能更改新/当前脚本的区域设置。我们还可以使用 setlocale() 函数的特定参数将语言环境信息设置为系统默认值。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法和参数

php setlocale() 的语法和参数如下:

Setlocale(constant1, location1);

setlocale() 的常量参数: 常量参数是强制参数,在 PHP 语言的 setlocale() 函数内部使用。它指定要设置的区域设置信息。有一些可用的常量参数值有时在 PHP 编程中非常有用。他们是:

  • LC_ALL: 意思是“以下所有”
  • LC_COLLATE: 意思是“排序”
  • LC_CTYPE:表示“字符转换和分类”(例如:所有字符都显示大写或小写)
  • LC_MESSAGES:表示“系统消息格式/格式化”
  • LC_MONETARY: 意思是“货币/货币格式”
  • LC_NUMERIC: 意思是“数字/数字格式”
  • LC_TIME: 意思是“时间和日期格式”

setlocale() 的位置参数: PHP 的 setlocale() 函数的位置参数也是 setlocale() 函数中应该使用的重要且强制的参数。它可以轻松指定要将哪个地区/国家设置为区域设置信息。它可以是数组或字符串。只能传递到多个位置。如果位置为 NULL 或空字符串 (“”),则位置值/名称将从与上述常量同名的环境变量值或“LANG”中设置。如果位置值设置为值“0”,则位置的设置不会受到影响,只会返回当前设置。

如果位置值是一个数组,setlocale() 函数将尝试每个数组元素,直到找到有效的区域代码或有效的语言。当且仅当该地区以许多不同的系统或名称为人所知时,这才非常有用。此 setlocale() 函数有许多语言代码可用。

setlocale() 函数在 PHP 中如何工作?

PHP 编程语言的 setlocale() 函数通常通过在两个强制参数的帮助下返回区域设置信息来工作。它只返回区域设置信息/信息。 setlocale() 函数的返回值是当前区域设置,但如果失败,将返回 FALSE。值/返回值将取决于实际运行的 PHP 系统。 setlocale() 需要 PHP 4.0+ 版本才能生成输出。在 PHP 5.3.0 版本中,如果将字符串内容传递给特定的常量参数而不是 LC_constants,则该函数将抛出 E_DREPRECATED 通知。

PHP setlocale() 示例

以下是示例:

示例#1

这是为“US”位置实现 setlocale() 函数的示例。首先创建 PHP 标签来输入我们想要实现的代码。然后在 echo 语句之后使用 hr 标签来打印水平线。然后使用字符串值“USA”创建“location1”变量。然后,location1 变量的值将在 echo 语句的帮助下打印出来。然后是“
”标签用于 echo 语句后打印换行符。然后在带有常量和位置参数的 echo 语句之后使用 PHP 编程语言的 setlocale() 函数。所以它会打印区域设置信息。然后“


”标签仅用于视图的水平线。

代码:

<?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>";
?>

输出:

PHP 设置区域设置()

Example #2

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 “


” 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 “
” 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.

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:

PHP 设置区域设置()

Example #3

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 “


” 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
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.

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:

PHP 设置区域设置()

以上是PHP 设置区域设置()的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn