PHP Reference Β· String Functions
PHP strcasecmp() function
Reference for PHP strcasecmp(): case-insensitive, binary-safe string comparison, return values, ASCII-only folding and use in sorting and lookups.
strcasecmp() compares two strings without regard to ASCII case. It works like strcmp(): a negative result means the first string sorts first, a positive result means it sorts second, and 0 means they are equal ignoring case.
Only the letters A to Z are folded to lowercase. Accented and non-Latin letters are compared as raw bytes, so "Γ" and "Γ©" differ. Only the sign of a non-zero result is reliable, not its size.
<?php var_dump(strcasecmp("Hello", "hELLO")); // int(0)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
strcasecmp(string $string1, string $string2): int
| Name | Description |
|---|---|
$string1 |
The first string. |
$string2 |
The second string. |
Return value: a negative number, 0 or a positive number. Test the sign, not the exact value.
Usage notes
- Do not use the result as a boolean.
strcasecmp($a, $b)is0, which is falsy, when the strings match. Compare with=== 0. - ASCII folding only, and since PHP 8.2 independent of the locale. For Unicode, lowercase both sides with
mb_strtolower()and compare with===. - Sorting: it is a valid callback for
usort(), but ties between"a"and"A"are reported as equal, so their relative order depends on the sort. - Only the first n characters:
strncasecmp()does the same for a prefix. - Natural ordering:
strnatcasecmp()ignores case and handles digit runs as numbers. - Searching with case ignored is done by
stripos(),str_contains()plus lowercase, orpreg_match()with theiflag.