strcasecmp – PHP String Functions
Syntax :
Description :
strcasecmp() function will compare two strings.
Parameter :
- string1 – This is a Required parameter. It is the first string which will be compared.
- string2 – This is a Required parameter. It is the second string which will be compared.
Output :
Return values in this function are:
- 0 – if the two strings are equal.
- < 0 – if string1 is less than string2.
- > 0 – if string1 is greater than string2.
Related articles : strcmp(), substr() , preg_match(), substr_compare(), stristr().
strcasecmp() – PHP Functions Example 1 : It returns 0, if the two strings are equal.
<?php echo strcasecmp("Hi from tutorialmines.","hI FROM TUTORIALMINES."); // case-insensitive comparison ?>
Output of above code in the browser is as below:
strcasecmp() – PHP Functions Example 2 : It returns 0, if the two strings are equal. Case of character does not matter for it.
<?php echo strcasecmp("Hi from tutorialmines.","hI FROM TUTORIALMINES."); echo "<br/>"; echo strcasecmp("Hi from tutorialmines.","hI from Tutorialmines."); ?>
Output of above code in the browser is as below:
0
strcasecmp() – PHP Functions Example 3 :
<?php echo "String1 is equal to string2<br/>"; echo strcasecmp("Hi from tutorialmines.","hI FROM TUTORIALMINES."); echo "<br/>String1 is greater than string2<br/>"; echo strcasecmp("Hi from tutorialmines.","hI Tutorialmines."); echo "<br/>String1 is less than string2<br/>"; echo strcasecmp("Hi from tutorialmines.","hI FROM TUTORIALMINES from PHP section."); ?>
Output of above code in the browser is as below:
0
String1 is greater than string2
-14
String1 is less than string2
14