: This article mainly introduces the strnatcmp function "natural sorting algorithm" in PHP to analyze the usage of string comparison and comparison strcmp functions. if you are interested in the PHP Tutorial, refer to it. This article describes how to use strnatcmp () function "natural sorting algorithm" in PHP to compare strings. We will share this with you for your reference. The details are as follows:
In PHP, the strnatcmp () function uses the "natural" algorithm to compare two strings (case sensitive). generally, in the natural algorithm, the number 2 is smaller than the number 10. In computer sorting, 10 is less than 2, because the first number in 10 is less than 2.
The strnatcmp () function is defined as follows:
Strnatcmp (string1, string2)
Parameter description:
String1 is required. Specifies the first string to be compared.
String2 is required. Specifies the second string to be compared.
Return value description:
If the two strings are equal, the return value is 0.
If string1 is less than string2, the return value is less than 0.
If string1 is greater than string2, the return value is greater than 0.
The sample code is as follows:
<? Php $ str1 = "2.jpg"; $ str2 =" 10.jpg"; $ str3 = "jb51.net _ 1"; $ str4 = "JB51.NET _ 2"; echo strcmp ($ str1, $ str2); // compare by byte, returns 1 echo"
"; Echo strcmp ($ str3, $ str4); // compare by byte, return 1 echo"
"; Echo strnatcmp ($ str1, $ str2); // compare by" natural sorting ", return-1 echo"
"; Echo strnatcmp ($ str3, $ str4); // compare by" natural sorting ", returns 1?>
The running result is as follows:
11-11