Open In App

PHP strcasecmp() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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:




<?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"
   
?>


Output:

0

Program 2: When the two strings are not identical:




<?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"
   
?>


Output:

-1

Program 3: When the two strings are not identical:




<?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"
   
?>


Output:

1

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



Last Updated : 21 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads