How to check if letter is upper or lower in PHP?

How to check if letter is upper or lower in PHP?

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

Handling Case Sensitivity in UTF-8 Text with PHP

Determining whether the first character of a string is uppercase or lowercase in PHP is a straightforward task for ASCII characters, but it becomes significantly more complex when dealing with UTF-8 encoded text containing diacritics. The core challenge lies not just in checking the byte value, but in understanding how Unicode defines case and how PHP's native functions handle these definitions.

When you encounter text like É, é, or Ê (which are often composed of a base letter plus a combining diacritical mark), a naive approach using mb_strtolower() or comparing ASCII ranges will fail. PHP's multibyte string extension, mb_*, is essential here, but even then, the definition of "uppercase" depends on whether you are checking the visual glyph or the underlying code point normalization form.

The most robust method involves leveraging mb_ord() to inspect the specific Unicode code point of the first character and comparing it against known uppercase/lowercase ranges, or utilizing PHP's built-in case conversion functions which handle normalization internally. However, relying solely on mb_strtolower($str) === $str is a common pattern that works well for most scenarios but has edge cases with certain capital letters that do not change form when lowercased (like the German Eszett ß in some contexts or specific stylistic variants).

Here is a practical implementation using mb_ord() to check the character directly:

function isFirstCharUppercase(string $text): bool {
    if ($text === '') {
        return false;
    }

    // Extract the first character
    $firstChar = mb_substr($text, 0, 1, 'UTF-8');

    // Get the Unicode code point of the first character
    $codePoint = mb_ord($firstChar);

    // Check against standard uppercase ranges for Latin characters.
    // This includes A-Z (65-90) and various accented uppercase letters 
    // that fall within their specific code point blocks.
    // Note: This is a simplified check; full normalization might be needed 
    // depending on strict requirements.
    
    return $codePoint >= 65 && $codePoint <= 122; 
}

Wait, the logic above is insufficient because it treats all characters between 65 and 122 as uppercase, ignoring that 'a' (97) is lowercase. A more accurate approach requires checking if the character is currently in its uppercase form or if converting it to lowercase yields a different string.

A safer, more idiomatic PHP pattern uses mb_strtolower() for comparison. This function handles normalization correctly for most UTF-8 inputs:

function isFirstCharUppercase(string $text): bool {
    if ($text === '') {
        return false;
    }

    $firstChar = mb_substr($text, 0, 1, 'UTF-8');
    
    // If converting to lowercase results in a different string, 
    // the original was considered uppercase by PHP's locale rules.
    return mb_strtolower($firstChar, 'UTF-8') !== $firstChar;
}

$text = "Éléphant";
echo isFirstCharUppercase($text) ? "Uppercase" : "Lowercase"; // Outputs: Uppercase

$text2 = "éléphant";
echo isFirstCharUppercase($text2) ? "Uppercase" : "Lowercase"; // Outputs: Lowercase

This approach avoids hardcoding code point ranges, which vary across different Unicode blocks (Greek, Cyrillic, etc.). By relying on PHP's internal Unicode data, the function adapts to the specific character set without manual range definition.

However, there is a critical pitfall to consider: composed vs. decomposed Unicode strings. If your input contains an accented character represented as a base letter followed by a combining diacritical mark (e.g., e + ´), mb_strtolower() might behave differently depending on the normalization form (NFC vs NFD). While PHP generally handles this gracefully, strict equality checks can occasionally yield unexpected results if the input encoding is inconsistent.

For high-security applications or strict compliance scenarios, you might need to normalize the string using normalizer_normalize() before applying the case check. This ensures that regardless of how the character was stored in the database, the logic operates on a consistent form. If you are building a framework where this check is ubiquitous, consider integrating these utilities into your core utility classes or leveraging existing libraries like Laravel's helpers if you are working within the ecosystem at https://laravelcompany.com.

Ultimately, the mb_strtolower() comparison method offers the best balance between simplicity and correctness for general UTF-8 handling. It respects locale rules implicitly and avoids the fragility of hardcoded ASCII ranges, making it suitable for internationalized applications where diacritics are common.

Our network