strcasecmp()
Our article is about the PHP function strcasecmp(), which is used to compare two strings without regard to case. In this article, we will discuss the syntax and
strcasecmp() is a built-in PHP function that compares two strings case-insensitively — meaning "Hello" and "hello" are treated as equal. It performs a binary-safe comparison and returns a number that tells you not just whether the strings differ, but which one is "greater" in byte order. This page covers the syntax, what the return value actually means, practical examples, common gotchas, and how it differs from related functions.
Syntax
strcasecmp(string $string1, string $string2): intIt takes two parameters, both required:
$string1— the first string to compare.$string2— the second string to compare.
Return value
This is the part most people get wrong. strcasecmp() does not return true/false. It returns an integer:
0if the two strings are equal (ignoring case).- A value less than 0 if
$string1is "less than"$string2. - A value greater than 0 if
$string1is "greater than"$string2.
The comparison is based on the byte values of the (lowercased) characters, so the result also gives you alphabetical ordering, which makes the function handy for sorting callbacks.
Basic example
Here $string1 and $string2 differ only in case, so strcasecmp() returns 0 and the condition $result == 0 is true.
The output of this code is:
The two strings are equal.Interpreting the sign of the result
When the strings are not equal, the sign tells you their order. Note that the exact magnitude is not standardized across PHP versions — only the sign (negative, zero, positive) is meaningful, so always compare against 0.
<?php
// "apple" comes before "Banana" alphabetically (case ignored)
var_dump(strcasecmp("apple", "Banana") < 0); // bool(true)
var_dump(strcasecmp("Banana", "apple") > 0); // bool(true)
var_dump(strcasecmp("PHP", "php") === 0); // bool(true)
?>Output:
bool(true)
bool(true)
bool(true)Practical use: case-insensitive login check
A common real-world use is comparing user input where case should not matter, such as a username or a yes/no answer.
<?php
$input = "ADMIN";
if (strcasecmp($input, "admin") === 0) {
echo "Welcome, admin!";
} else {
echo "Access denied.";
}
?>Output:
Welcome, admin!Using it as a sort comparator
Because it returns an ordering integer, strcasecmp() is a natural callback for usort() to sort strings alphabetically while ignoring case.
<?php
$names = ["banana", "Apple", "cherry", "apple"];
usort($names, "strcasecmp");
print_r($names);
?>Output:
Array
(
[0] => Apple
[1] => apple
[2] => banana
[3] => cherry
)Common gotchas
- It returns an integer, not a boolean. Writing
if (strcasecmp($a, $b))is a bug: the block runs when the strings are different (non-zero) and is skipped when they are equal (0). Always compare explicitly with=== 0. - It is byte-based, not Unicode-aware.
strcasecmp()only lowercases ASCII lettersA–Z. Accented or multibyte characters (likeÉvsé) are not treated as equal. For locale-aware multibyte comparison, normalize both strings first or use theintlextension. - Whitespace and trailing characters matter.
strcasecmp("yes", "yes ")is non-zero. Trim input withtrim()when needed.
Related functions
strcmp()— the case-sensitive counterpart; same return semantics.strncasecmp()— case-insensitive comparison of only the first n characters.strtolower()— lowercases a string, useful before a manual comparison.
Summary
strcasecmp() compares two strings without regard to case and returns 0 when they match, a negative number when the first is smaller, and a positive number when it is larger. Remember to test the result against 0 rather than treating it as a boolean, and reach for strcmp() when case should matter.