strcasecmp() Function Use in PHP 8.4 and PHP 8.5

strcasecmp() is fully available in PHP 8.4 and PHP 8.5 with no compatibility issues. It performs a binary safe, case insensitive string comparison (only ASCII letters are compared case insensitively).

Basic Usage & Output

Return values:

  • Returns < 0 if $string1 is less than $string2
  • Returns > 0 if $string1 is greater than $string2
  • Returns 0 if they are equal

Note: As of PHP 8.2.0, when lengths differ, the function no longer guarantees returning strlen($string1) - strlen($string2), but may return -1 or 1 instead.

Example with Output

<?php
$var1 = "Hello";
$var2 = "hello";

// Case-insensitive comparison: returns 0
var_dump(strcasecmp($var1, $var2)); // Output: int(0)

// Case-sensitive comparison for contrast
var_dump(strcmp($var1, $var2)); // Output: int(-32) (uppercase H is less than lowercase h )

// Unequal strings with different first letters
var_dump(strcasecmp("zappl", "apple")); // Output: int(25)
var_dump(strcasecmp("apple", "zappl")); // Output: int(-25)
?>

12 Views