strcasecmp() returns 0 when two strings are equal ignoring ASCII case, a negative value when the first sorts before the second, and a positive value when it sorts after.
if (strcasecmp('PHP', 'php') === 0) {
echo 'Same text ignoring case';
}Start with this current pattern. The examples below are retained, corrected where necessary, and expanded for additional variations and output.
echo strcasecmp("Hello world","HELLO WORLD"); // Output is 0
This is Binary safe case-insensitive compare two strings by using strcasecmp function.
$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
$str1 = 'Apple';
$str2 = 'apple';
echo strcmp($str1, $str2); // Output: -1 Non-zero (case-sensitive)
echo strcasecmp($str1, $str2); // Output: 0 (case-insensitive)
$str1 = '';
$str2 = 'test';
echo strcmp($str1, $str2); // Output: -1
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.