ctype_upper

Checks whether every character in a string is an uppercase letter.

Signature
ctype_upper(mixed $text): bool
  • Added in PHP 4.0.4

Parameters

ParameterTypeDescription
$textmixedThe string to test. Every byte must be an uppercase letter for the result to be true. An int between -128 and 255 is interpreted as an ASCII codepoint, which is why non-string arguments are deprecated as of PHP 8.1.

Return value

booltrue if every character in text is an uppercase letter in the current locale, false otherwise. An empty string always returns false.

Errors

Never throws. A non-string argument raises an E_DEPRECATED notice as of PHP 8.1; values that are neither string nor int return false.

Changelog

  • PHP 8.1Passing a non-string argument is deprecated. Ints between -128 and 255 are still read as ASCII codepoints, so cast to string or use chr() explicitly.

ctype_upper() is true when every character is an uppercase letter. As with its lowercase counterpart, the key word is letter — a digit, underscore or space makes it false, so ctype_upper('USD1') and ctype_upper('MAX_SIZE') both return false even though they contain no lowercase at all.

When to reach for it

  • Detecting shouted words in user-submitted text before flagging or normalising it.
  • Checking a currency or country code such as EUR or GB is properly capitalised.
  • Deciding whether an identifier follows a constant naming convention, after stripping underscores and digits.

What trips people up

Wanting "has no lowercase" and reaching for this function is the usual mistake. Compare with $s === strtoupper($s) instead, which is true for MAX_SIZE and for strings with no letters at all.

Single-letter checks are common and safe, but remember the byte-level limitation: under the default C locale only ASCII A-Z passes, so uppercase accented letters in UTF-8 return false. For Unicode, compare against mb_strtoupper().

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Flagging all-caps words in a message subject before sending it to moderation
  • Validating an ISO currency code is three uppercase letters
  • Checking a constant name follows the project's naming convention

Frequently asked questions

Why is ctype_upper false for MAX_SIZE?

The underscore is not an uppercase letter and every character must pass. Strip separators first, or compare with $s === strtoupper($s) if you only care that there is no lowercase.

What is the difference between ctype_upper and strtoupper?

ctype_upper tests and returns a bool; strtoupper converts and returns a string. Comparing $s with strtoupper($s) is the usual way to test for the absence of lowercase letters.

Does ctype_upper handle accented capitals such as É?

Not under the default C locale. The check is byte-based, so a multi-byte UTF-8 character fails; compare against mb_strtoupper for Unicode text.

More examples

Finding shouted words

Prints "URGENT, NOW" and then "2 shouted word(s)". Splitting on non-word characters first is what makes this work, since the colon and spaces would otherwise fail the whole-string test.

Example · php
<?php
$subject = 'URGENT: please Reply NOW';

$words = preg_split('/\W+/', $subject, -1, PREG_SPLIT_NO_EMPTY);
$shouted = array_filter($words, 'ctype_upper');

echo implode(', ', $shouted), "\n";
echo count($shouted), " shouted word(s)\n";

Validating a currency code

Prints true, false, false. The length check runs first so a valid-looking but wrong-length code is rejected for the right reason, and the empty string never reaches ctype_upper.

Example · php
<?php
function isCurrencyCode(string $code): bool
{
    return strlen($code) === 3 && ctype_upper($code);
}

var_dump(isCurrencyCode('EUR'));
var_dump(isCurrencyCode('eur'));
var_dump(isCurrencyCode('EURO'));

Discussion

  • Be the first to comment on this lesson.