What are the best practices for counting the number of uppercase letters in a text variable in PHP?

To count the number of uppercase letters in a text variable in PHP, you can use a combination of functions such as strlen() to get the length of the string, str_split() to split the string into an array of characters, and ctype_upper() to check if each character is uppercase. By iterating through the array of characters and checking if each character is uppercase, you can keep a count of the uppercase letters.

$text = "Hello World";
$uppercaseCount = 0;

$textArray = str_split($text);

foreach ($textArray as $char) {
    if (ctype_upper($char)) {
        $uppercaseCount++;
    }
}

echo "Number of uppercase letters: " . $uppercaseCount;