PHP strcasecmp() Function (original) (raw)

Last Updated : 11 Jul, 2025

The strcasecmp() function is a built-in function in PHP and is used to compare two given strings. It is case-insensitive. This function is similar to strncasecmp(), the only difference is that the strncasecmp() provides the provision to specify the number of characters to be used from each string for the comparison.Syntax:

strcasecmp($string1, $string2)

Parameters: This function accepts two mandatory parameters as shown in the above syntax and are described below: $string1, $string2: These parameters specify the strings to be compared. Return Value: This function returns an integer based on the conditions as described below:

Examples:

Input : $str1 = "Geeks for Geeks " $str2 = "Geeks for Geeks " Output : 0

Input : $str1 = "Geeks for Geeks" $str2 = "Hello Geek!" Output : -1

Below programs illustrate the strcasecmp() function in PHP:Program 1: When the two strings are identical:

php `

test=strcasecmp(test=strcasecmp(test=strcasecmp(str1, $str2); echo "$test"; ?>

`

Output:

0

Program 2: When the two strings are not identical:

php `

test=strcasecmp(test = strcasecmp(test=strcasecmp(str1, $str2); echo "$test"; ?>

`

Output:

-1

Program 3: When the two strings are not identical:

php `

str2 test=strcasecmp(test = strcasecmp(test=strcasecmp(str1, $str2); echo "$test"; ?>

`

Output:

1

Reference:https://www.php.net/manual/en/function.strcasecmp.php