PHP - lcfirst() 函式



PHP 的 lcfirst() 函式用於將給定字串的第一個字元轉換為小寫。 “小寫”指的是像 a、b、c……z 這樣的字母字元。

即使字串包含多個單詞,此函式也只會將給定字串中第一個單詞的第一個字元轉換為小寫。

例如,如果我們有字串“Hi, How are you?”,則結果字串將為“hi, How are you?”,而不是“hi, how are you?”。

語法

以下是 PHP lcfirst() 函式的語法:

lcfirst(string $str): string

引數

以下是此函式的引數:

  • str - 輸入字串。

返回值

此函式返回第一個字母為小寫的字串。

示例 1

以下是 PHP lcfirst() 函式的基本示例:

<?php
   $str = "Tutorialspoint";
   echo "The original string: $str";
   echo "\nThe resultant string: ";
   #using lcfirst() function
   echo lcfirst($str);
?>

輸出

以上程式產生以下輸出:

The original string: Tutorialspoint
The resultant string: tutorialspoint

示例 2

如果給定字串的第一個字元已經是小寫,則不會影響原始字串。

以下是使用 PHP lcfirst() 函式的另一個示例。我們使用此函式將此字串“Hello World”的第一個字元轉換為小寫:

<?php
   $str = "hello World";
   echo "The original string: $str";
   echo "\nThe resultant string: ";
   #using lcfirst() function
   echo lcfirst($str);
?>

輸出

執行上述程式後,將顯示以下輸出:

The original string: hello World
The resultant string: hello World
php_function_reference.htm
廣告