PHP strcasecmp Function | Case Insensitive String Comparison

PHP strcasecmp is an inbuilt Function in PHP. It compares two strings in a case-insensitive manner. Therefore, it treats lowercase and uppercase strings as the same values. The function compares and tells us if one string is greater, smaller or equal to the other string. In this article, we will discuss the PHP strcasecmp Function. Also, we will discuss a few examples of using it.

Note: You can use the PHP strcmp Function if you want to compare strings in a case-sensitive manner.


strcasecmp($string1, $string2)


The strcasecmp function in PHP expects two parameters. You need to pass both the strings you want to compare. Also, both parameters are mandatory. The description of the parameters is as follows:

  • string1: The first string for comparison.
  • string2: The second string for comparison.

Return Value

The function returns the following:

  • 0, if string1 = string2.
  • Returns a negative integer (< 0) , if string1 < string2.
  • Returns a positive integer (> 0), if string1 > string2.
PHP strcasecmp Function


Consider the following example:

// PHP program to illustrate the working of strcasecmp function
$string1 = "Welcome to Concatly"; 
$string2 = "Bringing Knowledge Together"; 
$string3 = "WELCOME TO CONCATLY"; 
// In this case both the strings are equal 
// $string1 is in lowercase and uppercase mix, while $string3 is all in uppercase. Still both the strings are same
print_r(strcasecmp($string1, $string3)); 
/* 0 */
// In this case the first is greater 
print_r(strcasecmp($string2, $string1)); 
/* -21 */
// In this case the second is greater 
print_r(strcasecmp($string3, $string2)) 
/* 21 */

You should observe in the above example, the function returns 0 when both the strings are same. Also, the function compares in a case-insensitive manner. It returns a negative value when string1 is smaller than string2. Also, it returns a positive value when string1 is greater than string2.


In conclusion, we discussed the PHP strcasecmp Function. You can read more about it on the Official PHP Documentation. Also, you can read about more PHP String Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *