PHP - ctype_lower() 函式



PHP 字元型別檢查ctype_lower()檢查給定的字串(文字)是否僅包含小寫字母。它還會檢查空格。如果字串包含任何空格,即使所有字母都是小寫,它也將被視為非小寫字元。

如果給定的字串僅由小寫字母組成,則此函式返回布林值true;否則,它返回false。如果給定的字串為空(""),則此函式始終返回false

語法

以下是 PHP 字元型別檢查ctype_lower()函式的語法 -

ctype_lower (mixed $text): bool

引數

此函式接受一個引數,如下所述 -

  • text(必填) - 需要測試的字串。

返回值

如果 text 中的每個字元都是小寫字母,則此函式返回“true”,否則返回“false”。

示例 1

如果提供的字串(或文字)中的每個字元都是小寫字母,則 PHP ctype_lower()函式返回 true -

<?php
   $string = "helloworld";
   echo "The given string is: $string";
   echo "\nDoes string '$string' consists only lowercase letters? ";
   #using ctype_lower() function
   var_dump(ctype_lower($string));
?>

輸出

以上程式產生以下輸出 -

The given string is: helloworld
Does string 'helloworld' consists only lowercase letters? bool(true)

示例 2

如果提供的字串(或文字)中的每個字元都不是小寫字母,則 PHP ctype_lower()函式返回 false -

<?php
   $string = "TutorialsPoint";
   echo "The given string is: $string";
   echo "\nDoes string '$string' consists only lowercase letters? ";
   #using ctype_lower() function
   var_dump(ctype_lower($string));
?>

輸出

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

The given string is: TutorialsPoint
Does string 'TutorialsPoint' consists only lowercase letters? bool(false)

示例 3

檢查多個字串(文字).

在下面的示例中,我們建立了一個包含多個字串的字串陣列,並使用 PHP ctype_lower()函式來確定字串中的每個字元是否都小寫 -

<?php
   $strings = array('Tutorialspoint', 'tutorix', "hello World");
   echo "The given strings are: ";
   foreach($strings as $text){
	   echo $text." ";
   }
   foreach ($strings as $test) {
      if (ctype_lower($test)) {
         echo "\nThe string '$test' consists of all lowercase letters.";
      }else {
         echo "\nThe string '$test' does not have all lowercase letters.";
      }
   }
?>

輸出

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

The given strings are: Tutorialspoint tutorix hello World
The string 'Tutorialspoint' does not have all lowercase letters.
The string 'tutorix' consists of all lowercase letters.
The string 'hello World' does not have all lowercase letters.

示例 4

如果提供的字串為空(""),則此函式始終返回false -

<?php
   $string = " ";
   echo "The given string is: $string";
   echo "\nDoes string '$string' consist only of lowercase letters? ";
   #using ctype_lower() function
   var_dump(ctype_lower($string));
?>

輸出

以下是上述程式的輸出 -

The given string is:
Does string ' ' consist only of lowercase letters? bool(false)
php_function_reference.htm
廣告