SoFunction
Updated on 2025-03-09

Summary of usage of php strnatcmp function

Definition and Usage
Definition and usage
The strnatcmp() function compares two strings using a "natural" algorithm.
The function of the strnatcmp() function is to use natural algorithms to compare strings (case sensitive).

In a natural algorithm, the number 2 is less than the number 10. In computer sorting, 10 is less than 2, because the first number in "10" is less than 2.
In the "Natural Algorithm Rules", 2 is smaller than 10. In a computer sequence, 10 is smaller than 2 because the first number in "10" is "1", which is less than 2.

This function returns:
This function will return the following values:

•0 - if the two strings are equal
0 – If the string is equal
•<0 - if string1 is less than string2
<0 – if string1 is less than string2
•>0 - if string1 is greater than string2
>0 – if string1 is greater than string2
Syntax
grammar

Copy the codeThe code is as follows:

strnatcmp(string1,string2)
Parameter parameters Description
string1 Required. Specifies the first string to compare
Required parameters. Specify the first string object participating in the comparison
string2 Required. Specifies the second string to compare
Required parameters. Specify the second string object for parameter comparison

Tips and Notes
Note
Note: The strnatcmp() is case-sensitive.
Note: The strnatcmp() function is case sensitive.

--------------------------------------------------------------------------------

Example
Case

Copy the codeThe code is as follows:

<?phpecho strnatcmp("2Hello world!","10Hello world!");echo "<br />";echo strnatcmp("10Hello world!","2Hello world!");?>

The output of the code above will be:
The above code will output the following result: -1 1