Tuesday, October 7, 2025
HomeLanguagesPHP strcasecmp() Function

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:




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

RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6708 POSTS0 COMMENTS
Nicole Veronica
11872 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6829 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6780 POSTS0 COMMENTS
Umr Jansen
6784 POSTS0 COMMENTS