W3docs

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
ParameterDescription
$string1The first string to compare.
$string2The 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:

ReturnMeaning
< 0 (negative)$string1 is less than $string2
> 0 (positive)$string1 is greater than $string2
0The two strings are equal

Basic example

php— editable, runs on the server

Because the numbers are compared by value (1 < 10), the output is:

file1.txt is less than file10.txt

Natural 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";   // positive

The 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.

FunctionNatural order?Case-sensitive?
strnatcasecmp()YesNo
strnatcmp()YesYes
strcasecmp()NoNo
strcmp()NoYes

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.

Practice

Practice
What is the role of the strnatcasecmp() function in PHP?
What is the role of the strnatcasecmp() function in PHP?
Was this page helpful?