PHP strnatcmp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, strnatcmp() compares two strings using natural order, and it is case-sensitive.

Numbers inside the text are compared by value, so "img2" is less than "img10", unlike a plain string comparison.

Syntax

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

PHP
$result = strnatcmp("img12.png", "img10.png");
CallWhat it does
strnatcmp("img12.png", "img10.png")Returns a positive number because 12 is greater than 10.
strnatcmp("Apple", "apple")Returns a negative number because uppercase comes first.
usort($items, "strnatcmp")Sorts an array in natural order.

For case-insensitive natural comparison, use strnatcasecmp(). Since PHP 8.2 the result is always -1, 0, or 1.

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

Parameters

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

Return Value

Return ValueDescription
intNegative if string1 is less than string2, 0 if they are equal, positive if string1 is greater.

PHP Examples

Example 1 — Compare Numbered Names

Natural order treats the numbers as numbers.

PHP
<?php
$result = strnatcmp("img12.png", "img10.png");

if ($result > 0) {
    echo "img12.png comes after img10.png", PHP_EOL;
}

How It Works

  1. Compare two file names that differ in a number.
  2. The text before the number is the same.
  3. PHP compares 12 and 10 as numbers, and 12 is bigger.
  4. The result is positive, so the first name comes later.

Example 2 — Case Matters

Unlike strnatcasecmp(), this function treats upper and lower case as different.

PHP
<?php
$result = strnatcmp("Apple", "apple");

echo $result < 0
    ? "Apple comes before apple"
    : "Apple does not come before apple", PHP_EOL;

How It Works

  1. Compare "Apple" with "apple".
  2. Uppercase "A" has a lower byte value than lowercase "a".
  3. The result is negative.
  4. Use strnatcasecmp() if you want these to be equal.

Example 3 — Sort with usort()

Use the function name as a sorting callback.

PHP
<?php
$items = ["img12.png", "img10.png", "img2.png", "img1.png"];

usort($items, "strnatcmp");

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

How It Works

  1. Start with a mixed-up list of numbered file names.
  2. Pass "strnatcmp" to usort as the comparison function.
  3. usort calls it for pairs of items and reorders them.
  4. The list ends up in natural order: 1, 2, 10, 12.

Frequently Asked Questions

It compares two strings using natural order. Numbers are compared by value, and letters are compared in a case-sensitive way.
strcmp() compares character by character, so 'img10' comes before 'img2'. strnatcmp() understands numbers and puts 'img2' first.
Yes. Uppercase letters sort before lowercase ones. Use strnatcasecmp() to ignore case.
A negative number, 0, or a positive number. From PHP 8.2 it is always -1, 0, or 1.
Use usort($array, 'strnatcmp'). You can also call natsort($array), which does natural sorting and keeps the keys.

Next: strncasecmp()

strncasecmp() compares the first N characters of two strings, ignoring case.

strncasecmp() 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