strcmp()
The strcmp() function in PHP is used to compare two strings. It compares two strings lexicographically, which means that it compares the two strings character
Introduction
strcmp() is PHP's built-in function for comparing two strings byte by byte ("binary-safe" comparison). Rather than answering "are these equal?" with a boolean, it returns an integer that also tells you their relative order, which is exactly what sorting algorithms need. This page covers the syntax, what the return value means, the gotchas that trip people up, and when to reach for a related function instead.
Syntax
strcmp(string $string1, string $string2): intIt takes the two strings to compare and returns an int:
| Return value | Meaning |
|---|---|
0 | The two strings are exactly equal. |
< 0 (negative) | $string1 sorts before $string2. |
> 0 (positive) | $string1 sorts after $string2. |
Only the sign is guaranteed. The magnitude of the result is an implementation detail (it is often, but not always, the ASCII difference of the first non-matching byte), so always test the sign — never compare against a specific number like
== -1or== 1.
The comparison is case-sensitive and based on byte values. Because uppercase ASCII letters (A–Z, 65–90) come before lowercase ones (a–z, 97–122), "Z" is considered "less than" "a".
Basic example
"Hello" and "World" differ at the very first character: H (72) versus W (87). Since H comes first, strcmp() returns a negative number and the second branch runs, printing The first string is less than the second string.
How the return value is decided
strcmp() walks both strings one byte at a time and stops at the first position where they differ, returning the sign of that byte difference. If one string is a prefix of the other, the shorter string is "less than" the longer one.
<?php
var_dump(strcmp("apple", "apple")); // int(0) — identical
var_dump(strcmp("apple", "apples")); // negative int — "apple" is shorter (a prefix)
var_dump(strcmp("apple", "Apple")); // positive int — 'a'(97) > 'A'(65)
var_dump(strcmp("abc", "abd")); // negative int — differ at 3rd char: 'c' < 'd'That third case is the classic gotcha: "apple" and "Apple" are not equal because the comparison is case-sensitive.
A common mistake: comparing for equality
strcmp() returns 0 when strings match, and 0 is falsy in PHP. So this condition is backwards:
<?php
$a = "secret";
$b = "secret";
// WRONG: this block runs only when the strings are DIFFERENT
if (strcmp($a, $b)) {
echo "match"; // never printed for equal strings
}
// RIGHT: test explicitly against 0
if (strcmp($a, $b) === 0) {
echo "match"; // prints "match"
}If you only need to know whether two strings are equal (not their order), the simple === operator is clearer and faster: $a === $b.
Sorting with strcmp()
The real strength of strcmp() is as a comparator. usort() expects a callback that returns a negative, zero, or positive value — exactly strcmp()'s contract:
<?php
$fruits = ["banana", "Apple", "cherry", "apple"];
usort($fruits, "strcmp");
print_r($fruits);
// Array ( [0] => Apple [1] => apple [2] => banana [3] => cherry )Uppercase "Apple" lands first because A (65) sorts before all lowercase letters. For human-friendly, case-insensitive ordering use strcasecmp() instead.
Related functions
strcasecmp()— same asstrcmp()but case-insensitive.strncmp()— compares only the first n bytes of each string.strcoll()— locale-aware comparison (respects the current locale's collation rules).substr_compare()— compares strings starting from a given offset.strpos()— find the position of a substring rather than compare whole strings.
Summary
strcmp() performs a binary-safe, case-sensitive comparison of two strings and returns 0 when they are equal, a negative number when the first sorts earlier, and a positive number when it sorts later. Always check the sign of the result (not a literal like -1), remember that 0 is falsy so equality needs an explicit === 0, and reach for strcasecmp(), strncmp(), or strcoll() when you need case-insensitive, partial, or locale-aware comparisons.