Case insensitive string comparison

This is Binary safe case-insensitive compare two strings by using strcasecmp function.
echo strcasecmp("Hello world","HELLO WORLD");
Output is equal to
  • 0 : if both strings are equal or matching
  • < 0 : if string 1 is less than string 2
  • > 0 : if string 1 is greater than string 1


For case sensitive string matching use strcmp function

Here is a sample code using strcasecomp function to check two strings.

$str1="Hello World";
$str2="hello world";
echo strcasecmp($str1,$str2); // Output is 0
if(strcasecmp($str1,$str2)==0){
echo "Both strings are matching";
}else{
echo "Both strings are different ";
}
Output is
Both strings are matching
Example 2
$str1="Hello";
$str2="hello world";
echo strcasecmp($str1,$str2);  // Output is -6
if(strcasecmp($str1,$str2)==0){
echo "Both strings are matching";
}else{
echo "Both strings are different ";
}
Output is
Both strings are different 
Example 3
$str1="Hello World";
$str2="hello";
echo strcasecmp($str1,$str2);  // Output is 6
if(strcasecmp($str1,$str2)==0){
echo "Both strings are matching";
}else{
echo "Both strings are different ";
}
Both strings are different 
Your Rating





PHP String Functions

Post your comments , suggestion , error , requirements etc here .




HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer