strcasecmp(): Case insensitive string comparison

Compare strings without case sensitivity

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';}
echo strcasecmp("Hello world","HELLO WORLD"); // Output is 0
This performs a binary-safe comparison ignoring ASCII letter case 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 2


Case sensitive string comparison use strcmp() function

These examples show equality and ordering checks without relying on an exact nonzero return value.
Example 1
$str1 = "Hello World";$str2 = "hello world";$result = strcasecmp($str1, $str2); // 0
if ($result === 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";$result = strcasecmp($str1, $str2);if ($result < 0) {
    echo "First string sorts before the second" . PHP_EOL;}if ($result !== 0) {
    echo "Both strings are different";}
Output is
First string sorts before the second
Both strings are different
Example 3
$str1 = "Hello World";$str2 = "hello";$result = strcasecmp($str1, $str2);if ($result > 0) {
    echo "First string sorts after the second" . PHP_EOL;}if ($result !== 0) {
    echo "Both strings are different";}
First string sorts after the second
Both strings are different

Example: Case-Sensitive vs Case-Insensitive Comparison

$str1 = 'Apple';$str2 = 'apple';var_dump(strcmp($str1, $str2) !== 0); // true (case-sensitive)
var_dump(strcasecmp($str1, $str2) === 0); // true (ignores ASCII case)

Example: Handling Empty Strings

$str1 = '';$str2 = 'test';var_dump(strcmp($str1, $str2) < 0); // true

String Functions strcmp(): Case sensitive string comparison str_replace(): String replace


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-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer