Case sensitive string comparison can be done by using strcmp function in PHP.
echo strcmp("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 a case in sensitive string comparison use strcasecmp function
Here is a sample code using strcomp function to check two strings.
Example 1
$str1="Hello World";
$str2="hello world";
echo strcmp($str1,$str2); // Output is -1
echo "<br>";
if(strcmp($str1,$str2)==0){
echo "Both strings are matching";
}else{
echo "Both strings are different ";
}
Output is
-1
Both strings are different
Example 2
$str1="Hello World";
$str2="Hello World";
echo strcmp($str1,$str2); // Output is 0
echo "<br>";
if(strcmp($str1,$str2)==0){
echo "Both strings are matching";
}else{
echo "Both strings are different ";
}
Output is here
0
Both strings are matching
← Subscribe to our YouTube Channel here