Which PHP function can be used to check if a string is entirely lowercase?

Solution:

If you just need a simple check, compare the original string with its lowercase version:


function check_lowercase_string($string) {
    return ($string === strtolower($string));
}

$string = 'Hi! I am a string';
var_dump(check_lowercase_string($string)); // false
var_dump(check_lowercase_string('test'));  // true

This works in most cases, but it’s not foolproof (e.g., with multibyte characters).

A “no built-in functions” attempt

If you want to avoid strtolower() and similar functions, you can manually check character by character:


function check_lowercase_string($string) {
    $small = [];
    // map all lowercase letters
    for ($alpha = 'a'; $alpha != 'aa'; $alpha++) {
        $small[] = $alpha;
    }

    $chars = '';
    $l = 0;

    // count length manually
    while (@$string[$l] != '') {
        $l++;
    }

    // filter out only lowercase chars
    for ($i = 0; $i < $l; $i++) {
        foreach ($small as $letter) {
            if ($string[$i] == $letter) {
                $chars .= $letter;
            }
        }
    }

    // if filtered string matches input, it’s all lowercase
    return ($chars === $string);
}

var_dump(check_lowercase_string('teSt')); // false

✅ First method is practical.
🤓 Second one is more of an exercise in “no built-ins.”