PHP | strcasecmp() Function

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:



  • strcasecmp() returns 0 – if the two strings are equal.
  • strcasecmp() returns < 0 – if string1 is less than string2
  • strcasecmp() returns > 0 – if string1 is greater than string2

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the use
// of strcasecmp() function
  
$str1 = "Geeks for Geeks ";
$str2 = "Geeks for Geeks ";
   
// Both the strings are equal
$test=strcasecmp($str1, $str2); 
   
echo "$test"
   
?>

chevron_right


Output:

0

Program 2: When the two strings are not identical:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the use
// of strcasecmp() function
  
$str1 = "Geeks for Geeks";
$str2 = "Hello Geek!";
   
// Both the strings are not equal
//  str1 < str2 
  
$test = strcasecmp($str1, $str2); 
   
echo "$test"
   
?>

chevron_right


Output:

-1

Program 3: When the two strings are not identical:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the use
// of strcasecmp() function
$str1 = "Hello Geek!";
$str2 = "Geeks for Geeks";
  
   
// Both the strings are not equal
//  str1 > str2 
$test = strcasecmp($str1, $str2); 
   
echo "$test"
   
?>

chevron_right


Output:

1

Reference:
http://php.net/manual/en/function.strcasecmp.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : anupamsworld

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.