ctype_lower

Checks whether every character in a string is a lowercase letter.

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

Parameters

ParameterTypeDescription
$textmixedThe string to test. Every byte must be a lowercase 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 a lowercase 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_lower() is true when every character is a lowercase letter. The important word is letter: this is not "contains no uppercase". Digits, hyphens, underscores and spaces all make it return false, so ctype_lower('blog-post') and ctype_lower('user1') are both false.

When to reach for it

  • Deciding whether a token in a parser is a lowercase keyword.
  • Enforcing a naming convention on a purely alphabetic identifier.
  • Detecting whether a string has already been normalised, so you can skip a strtolower() call in a hot loop — though the call is cheap enough that the check rarely pays for itself.

What trips people up

If you actually want "contains no uppercase letters", compare instead: $s === strtolower($s). That is true for blog-post and for the empty string, which is usually what people mean.

The check is also locale- and byte-sensitive. Under the default C locale only ASCII a-z counts, so accented lowercase letters in UTF-8 return false.

Example

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

When to use it

  • Validating that a tag or label follows an all-lowercase alphabetic convention
  • Detecting whether a header name has already been normalised to lowercase
  • Classifying single characters while walking a string in a tokeniser

Frequently asked questions

Why does ctype_lower return false for a string with numbers in it?

Because every character must be a lowercase letter, and a digit is not a letter. ctype_lower('abc1') is false; use ctype_alnum or a regex if digits should be allowed.

How do I check that a string contains no uppercase letters?

Compare the value with its lowercased form: $s === strtolower($s). That treats digits, punctuation and the empty string as acceptable, which ctype_lower does not.

Does ctype_lower work with accented letters like é?

Not under the default C locale. The function tests raw bytes, and a UTF-8 é is two non-ASCII bytes, so the string returns false. Use mb_strtolower and a comparison for Unicode text.

More examples

Checking a header name is already normalised

Prints normalised, needs strtolower, normalised. The hyphen has to be stripped first, because ctype_lower rejects any character that is not a lowercase letter.

Example · php
<?php
$headers = ['content-type', 'Content-Length', 'accept'];

foreach ($headers as $header) {
    $letters = str_replace('-', '', $header);
    echo $header, ': ',
        ctype_lower($letters) ? "normalised\n" : "needs strtolower\n";
}

Counting lowercase letters in a password

Prints "lowercase letters: 7" then "meets policy" — 'Str0ngPass!' contains t, r, n, g, a, s and s. Per-character testing is how strength rules are normally built, since the whole-string result would be false for any mixed password.

Example · php
<?php
$password = 'Str0ngPass!';
$lower = 0;

foreach (str_split($password) as $ch) {
    if (ctype_lower($ch)) {
        $lower++;
    }
}

echo "lowercase letters: $lower\n";
echo $lower >= 3 ? "meets policy\n" : "too few\n";

Discussion

  • Be the first to comment on this lesson.