This article mainly introduces the use of strnatcmp () function "natural sorting algorithm" in PHP for string comparison, and analyzes the use techniques of strnatcmp function string comparison based on the instance form, the strcmp function is compared to demonstrate the differences between the two. For more information, see the example in this article. the strnatcmp () function "natural sorting algorithm" in PHP is used to compare strings. We will share this with you for your reference. The details are as follows:
PHPThe strnatcmp () function uses the "natural" algorithm to compare two strings (case sensitive). generally, in the natural algorithm, number 2 is smaller than 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.
Sample codeAs follows:
<? Php $ str1 = "2.jpg"; $ str2 =" 10.jpg"; $ str3 = "bitsCN.com _ 1"; $ str4 = "BITSCN. COM_2 "; echo strcmp ($ str1, $ str2); // compare by byte. 1 echo is returned"
"; 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