PHP 8 reference · String Functions

PHP strnatcasecmp() Function

Compare strings in natural order ("a2" before "a10") while ignoring upper and lower case.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

strnatcasecmp(string $string1, string $string2): int

Parameters

ParameterDescription
string1 The first string.
string2 The second string.

Return value

0 if equal; negative or positive otherwise.

Examples

Natural, case-insensitiveTry it yourself
<?php
var_dump(strnatcasecmp("Chapter10", "chapter9") > 0);

Output

bool(true)
Sort mixed-case namesTry it yourself
<?php
$items = ["Item20", "item3", "ITEM100"];
usort($items, "strnatcasecmp");
print_r($items);

Output

Array
(
    [0] => item3
    [1] => Item20
    [2] => ITEM100
)

Version history

VersionChange
PHP 4.0Added

Was this page helpful?