PHP strcasecmp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, strcasecmp() compares two strings without caring about upper or lower case.

It returns 0 when the strings are equal, and a negative or positive number otherwise.

Syntax

Call strcasecmp() with the values you need. Start with this line.

PHP
$result = strcasecmp($a, $b);
CallWhat it does
strcasecmp("Hello", "hello")Returns 0 (equal).
strcasecmp("apple", "Banana")Returns a negative number.

Since PHP 8.2 the result is always -1, 0, or 1. Older versions may return other negative or positive numbers.

Library declarations
PHP
strcasecmp(string $string1, string $string2): int

Parameters

ParameterDescription
string1Required. The first string.
string2Required. The second string.

Return Value

Return ValueDescription
intLess than 0 if string1 is less than string2, 0 if equal, greater than 0 if string1 is greater.

PHP Examples

Example 1 — Ignore Letter Case

Two strings that differ only in case count as equal.

PHP
<?php
$result = strcasecmp("Hello", "hello");

if ($result === 0) {
    echo "Equal", PHP_EOL;
} else {
    echo "Different", PHP_EOL;
}

How It Works

  1. Pass two strings. They differ only in the first letter's case.
  2. Case is ignored. H and h match.
  3. Get 0. That means the strings are equal.
  4. Check with === 0. Do not rely on a plain if.

Example 2 — Which String Comes First?

A negative result means the first string sorts earlier.

PHP
<?php
if (strcasecmp("apple", "Banana") < 0) {
    echo "apple comes first", PHP_EOL;
} else {
    echo "Banana comes first", PHP_EOL;
}

How It Works

  1. Compare words. apple and Banana start with different letters.
  2. Case is ignored. It compares a with b.
  3. Get a negative number. a comes before b.
  4. Test with < 0. Use the sign instead of an exact number.

Example 3 — Sort a List Without Caring About Case

Pass the function name to usort().

PHP
<?php
$names = ["banana", "Apple", "cherry"];

usort($names, "strcasecmp");

echo implode(", ", $names), PHP_EOL;

How It Works

  1. Start with mixed case. A plain sort would put Apple first anyway, but not always.
  2. Pass the function name. usort() calls it for each pair.
  3. Case is ignored. Words are ordered alphabetically.
  4. Print the list. implode() joins the items.

Frequently Asked Questions

strcasecmp() compares two strings byte by byte while ignoring upper and lower case. It returns 0 when they are equal.
strcasecmp(string $string1, string $string2): int. Pass the two strings you want to compare.
strcmp() is case-sensitive. strcasecmp() ignores case, so "PHP" and "php" are equal.
Equal strings return 0, which is falsy. Always compare the result with === 0 to test for equality.
Not well. It works on bytes and ASCII letters. For multibyte text, use mb_strtolower() on both strings and compare them.

Next: strchr()

strchr() finds the first match of a substring and returns the text from there to the end.

strchr() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful