Sample code using strcmp 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
Example: Case-Insensitive String Comparison
$result = strcasecmp('Hello', 'hello');
if ($result === 0) {
echo 'The strings are equal (case-insensitive).';
} else {
echo 'The strings are not equal.';
}
Output
The strings are equal (case-insensitive).
Example: Comparing Strings with Different Encodings
$str1 = mb_convert_encoding('Straße', 'UTF-8', 'ISO-8859-1');
$str2 = 'Strasse';
if (strcmp($str1, $str2) === 0) {
echo "The strings are equal.";
} else {
echo "The strings are not equal.";
}