echo strcmp("Hello world","HELLO WORLD"); // Output is 1
Output is equal to
$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
$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).
$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.";
}
Output
The strings are not equal.
These examples improve the depth of string comparison by considering case insensitivity and encoding differences.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.