PHP » Strings » strcasecmp()

Syntax:
int strcasecmp(string s1, string s2)
s1
The first string.
s2
The second string.

Case insensitive comparison of two strings.

This function works like strcmp(), but is case insensitive. If the first string is "smaller", in the sorting sense, -1 is returned. 1 is returned if the the first string is larger in value.

Examples

Code:
<?php

$s1 = "abc";
$s2 = "def";
$s3 = "XYZ";
$s4 = "ABC";

if (strcasecmp($s1, $s2) < 0) {
   print "$s1 is smaller than $s2<br>";
} else {
   print "$s2 is smaller than $s1<br>";
}

if (strcasecmp($s1, $s3) < 0) {
   print "$s1 is smaller than $s3<br>";
} else {
   print "$s3 is smaller than $s1<br>";
}

if (strcasecmp($s1, $s4) == 0) {
   print "$s1 is equal to $s4";
} else {
   print "$s1 and $s4 are not equal";
}

?>
Output:
abc is smaller than def
abc is smaller than XYZ
abc is equal to ABC
Explanation:

Despite different ASCII values, lower and upper case characters are valued equally.

See Also: