strcasecmp(): Case insensitive string comparison

echo strcasecmp("Hello world","HELLO WORLD"); // Output is 0
This is Binary safe case-insensitive compare two strings by using strcasecmp function.

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


Case sensitive string comparison use strcmp() function

Sample code using strcasecmp function to check two strings.
Example 1

$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 

Example: Case-Sensitive vs Case-Insensitive Comparison

$str1 = 'Apple';
$str2 = 'apple';
echo strcmp($str1, $str2);  // Output: -1  Non-zero (case-sensitive)
echo strcasecmp($str1, $str2);  // Output: 0 (case-insensitive)

Example: Handling Empty Strings

$str1 = '';
$str2 = 'test';
echo strcmp($str1, $str2);  // Output: -1

String Functions strcmp(): Case sensitive string comparison str_replace(): String replace
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer