Performing Case-Insensitive String Comparisons in PHP with strcasecmp


  • Return Values
    The function returns an integer value based on the comparison result:
    • 0 - If the two strings are identical (case-insensitive).
    • -1 (negative value) - If the first string is less than the second string (after considering lowercase conversion).
    • 1 (positive value) - If the first string is greater than the second string (after considering lowercase conversion).
  • Case-Insensitive
    During the comparison, it converts both strings to lowercase letters before doing the actual comparison. This ensures that "Hello" and "hELLO" will be considered equal.
  • Functionality
    It takes two strings as arguments and compares them character by character.
<?php

$string1 = "Hello, World!";
$string2 = "hElLo, wOrLd!";

$result = strcasecmp($string1, $string2);

if ($result == 0) {
  echo "The strings are equal (case-insensitive)";
} elseif ($result > 0) {
  echo "String '$string1' is greater than '$string2' (case-insensitive)";
} else {
  echo "String '$string1' is less than '$string2' (case-insensitive)";
}

?>

This code will output:

The strings are equal (case-insensitive)

As you can see, even though the strings have different letter cases, strcasecmp considers them equal because their lowercase counterparts are identical.

Here are some additional points to keep in mind about strcasecmp:

  • It is similar to the strcmp function, but strcmp is case-sensitive.


Checking for specific case-insensitive match

<?php

$username = "admin";
$userInput = "AdMiN";

if (strcasecmp($username, $userInput) === 0) {
  echo "Login successful!";
} else {
  echo "Incorrect username or password.";
}

?>

This code checks if the entered username (userInput) matches the stored username (username) regardless of case.

Finding the "smaller" string (case-insensitive)

<?php

$string1 = "Apple";
$string2 = "banana";

$smallerString = strcasecmp($string1, $string2) < 0 ? $string1 : $string2;

echo "The smaller string (case-insensitive) is: $smallerString";

?>

This code uses the return value of strcasecmp to determine which string comes "first" alphabetically (ignoring case).

Sorting an array of strings (case-insensitive)

<?php

$fruits = array("Orange", "apple", "Mango");

usort($fruits, function($a, $b) {
  return strcasecmp($a, $b);
});

print_r($fruits);

?>

This code utilizes the usort function with a custom comparison function based on strcasecmp to sort the fruits array in a case-insensitive manner.



Lowercase Conversion + strcmp

This is a simple approach where you convert both strings to lowercase before using the case-sensitive strcmp function. It's less efficient than strcasecmp but might be suitable for very short strings.

<?php

$string1 = "Hello";
$string2 = "hELLO";

$lower1 = strtolower($string1);
$lower2 = strtolower($string2);

$result = strcmp($lower1, $lower2);

// Logic based on $result value (similar to strcasecmp)
?>

mb_strtolower + strcmp

This option is similar to the previous one, but it uses mb_strtolower which is more robust for handling multibyte characters with proper encoding awareness.

Regular Expressions (preg_match)

If you need more complex case-insensitive matching with patterns, you can leverage regular expressions with the preg_match function. However, this approach requires understanding regular expressions and might be less performant for simple comparisons.

Locale-Aware Comparison (strcoll)

For a more sophisticated comparison considering locale-specific sorting rules (e.g., accents), you can use strcoll. This is useful for internationalization scenarios.

  • For locale-aware sorting, explore strcoll.
  • For complex pattern matching, utilize regular expressions with caution for performance.
  • If you need multibyte character handling, consider mb_strtolower + strcmp.
  • For simple case-insensitive comparisons, strcasecmp remains the most efficient and recommended choice.