strnatcasecmp()
The strnatcasecmp() function in PHP is used to compare two strings case-insensitively using a "natural order" algorithm. It is similar to the strcasecmp()
Introduction
strnatcasecmp() compares two strings using a "natural order" algorithm, ignoring case. "Natural order" means numbers embedded in the strings are compared by their numeric value rather than character by character — so "file2" sorts before "file10", which is how a person would order them. The plain alphabetical comparison used by strcasecmp() would instead put "file10" first, because '1' comes before '2'.
This page covers the function's signature, its return value, how it differs from related comparison functions, and how to use it as a sorting callback.
Syntax
strnatcasecmp(string $string1, string $string2): int| Parameter | Description |
|---|---|
$string1 | The first string to compare. |
$string2 | The second string to compare. |
The function is binary-safe (it works with strings that contain null bytes) and case-insensitive ("FILE" and "file" are treated as equal).
Return value
strnatcasecmp() returns an int whose sign tells you the result — the exact magnitude is implementation-defined, so always test against 0, not a specific number:
| Return | Meaning |
|---|---|
< 0 (negative) | $string1 is less than $string2 |
> 0 (positive) | $string1 is greater than $string2 |
0 | The two strings are equal |
Basic example
Because the numbers are compared by value (1 < 10), the output is:
file1.txt is less than file10.txtNatural vs. regular comparison
This is the whole reason strnatcasecmp() exists. With a regular comparison, strings are compared one character at a time, so '1' (the first digit of 10) beats '2'. Natural order looks at the full number:
<?php
$a = "img12";
$b = "IMG2";
// Regular, case-insensitive: '1' < '2', so img12 comes first
echo strcasecmp($a, $b) . "\n"; // negative
// Natural, case-insensitive: 12 > 2, so img12 comes second
echo strnatcasecmp($a, $b) . "\n"; // positiveThe two functions disagree on order and strnatcasecmp() ignores the IMG vs img casing, returning the human-friendly result.
Sorting an array naturally
The most common real-world use is as the comparison callback for usort(), to sort filenames or version-like strings:
<?php
$files = ["file10.txt", "File2.txt", "file1.txt"];
usort($files, "strnatcasecmp");
print_r($files);Output:
Array
(
[0] => file1.txt
[1] => File2.txt
[2] => file10.txt
)If you have an array you want to sort in place by value, natcasesort() applies the same natural, case-insensitive ordering while preserving keys.
Related functions
| Function | Natural order? | Case-sensitive? |
|---|---|---|
strnatcasecmp() | Yes | No |
strnatcmp() | Yes | Yes |
strcasecmp() | No | No |
strcmp() | No | Yes |
Use strnatcasecmp() when you need both natural ordering and case-insensitivity; pick strnatcmp() if case matters.
Conclusion
strnatcasecmp() compares two strings in the order a human would expect — treating embedded numbers as numbers and ignoring letter case. Always branch on the sign of its return value, and reach for it (often together with usort()) whenever you sort filenames, labels, or any strings that mix text and numbers.