PHP - ctype_upper() 函式



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

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

語法

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

ctype_upper (mixed $text): bool

引數

此函式接受以下引數:

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

返回值

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

示例 1

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

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

輸出

上述程式產生以下輸出:

The given string is: TUTORIALSPOINT
Does string 'TUTORIALSPOINT' consists only uppercase letters? bool(true)

示例 2

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

<?php
   $string = "Hello World";
   echo "The given string is: $string";
   echo "\nDoes string '$string' consists only uppercase letters? ";
   #using ctype_upper() function
   var_dump(ctype_upper($string));
?>

輸出

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

The given string is: Hello World
Does string 'Hello World' consists only uppercase letters? bool(false)

示例 3

檢查多個字串(文字).

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

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

輸出

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

The given strings are: Tutorialspoint TUTORIX INDIA
The string 'Tutorialspoint' does not have all uppercase letters.
The string 'TUTORIX' consists of all uppercase letters.
The string 'INDIA' consists of all uppercase letters.

示例 4

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

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

輸出

以下是上述程式的輸出:

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