PHP 設定區域設定()

PHPz
發布: 2024-08-29 12:51:02
原創
926 人瀏覽過

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中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!