In PHP, checking whether a string consists entirely of uppercase letters is a common operation. Fortunately, PHP provides the built-in function ctype_upper(), which makes this task easy. In this article, we’ll show you how to use the ctype_upper() function to determine if a string is made up entirely of uppercase letters.
ctype_upper() is a very useful character classification function in PHP. It checks whether all characters in a given string are uppercase letters. The function's prototype is as follows:
bool ctype_upper ( string $text )
Parameter: The function accepts a single string parameter $text, which is the target string to check.
Return Value: Returns true if all characters in the string are uppercase letters; otherwise, it returns false.
Note that ctype_upper() only evaluates alphabetic characters. If the string contains non-alphabetic characters, it will return false immediately.
Suppose we want to check whether a user-provided string is made up entirely of uppercase letters. Here's how we could do it:
<?php
// Test string
$str = "HELLOWORLD";
<p>// Use ctype_upper to check if the string is all uppercase<br>
if (ctype_upper($str)) {<br>
echo "The string is all uppercase letters.\n";<br>
} else {<br>
echo "The string is not all uppercase letters.\n";<br>
}<br data-is-only-node="">
?><br>
Output:
The string is all uppercase letters.
If the string includes non-alphabetic characters such as digits, symbols, or spaces, ctype_upper() will return false. Here’s an example with numbers and spaces:
<?php
$str = "HELLO WORLD 123";
<p>// Use ctype_upper to check if the string is all uppercase<br>
if (ctype_upper($str)) {<br>
echo "The string is all uppercase letters.\n";<br>
} else {<br>
echo "The string is not all uppercase letters.\n";<br>
}<br data-is-only-node="">
?><br>
Output:
The string is not all uppercase letters.
If you want to ignore spaces or special characters and only check if the alphabetic portion is uppercase, you can first remove non-alphabetic characters using preg_replace() before checking:
<?php
$str = "HELLO WORLD 123";
<p>// Remove non-letter characters<br>
$filteredStr = preg_replace('/[^a-zA-Z]/', '', $str);</p>
<p>// Check if the letter-only part is all uppercase<br>
if (ctype_upper($filteredStr)) {<br>
echo "The letter portion is all uppercase.\n";<br>
} else {<br>
echo "The letter portion is not all uppercase.\n";<br>
}<br data-is-only-node="">
?><br>
Output:
The letter portion is all uppercase.
In this example, we used the regular expression /[^a-zA-Z]/ to strip all non-letter characters, then used ctype_upper() to evaluate the result.
The ctype_upper() function is an efficient and easy-to-use tool in PHP for checking whether a string is entirely made up of uppercase letters. A simple call to this function can handle the task. However, keep in mind that it does not ignore non-letter characters — if the string contains digits, symbols, or spaces, the function will return false.
We hope this simple example helps you understand how to use ctype_upper() in PHP to determine if a string is entirely uppercase. If you want to ignore other characters, you can preprocess the string using a regular expression.