The phpstrnatcmp () function uses a natural algorithm to compare two strings. This article introduces the phpstrnatcmp () function usage and basic use examples to the coders. if you need them, refer to this article. Definition and usage
The strnatcmp () function uses a "natural" algorithm to compare two strings.
In natural algorithms, 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.
Note: This function is case sensitive.
Syntax
strnatcmp(string1,string2)
Parameters |
Description |
String1 |
Required. Specifies the first string to be compared. |
String2 |
Required. Specifies the second string to be compared. |
Return value
Return value: |
This function returns:
- 0-if the two strings are equal
- <0-ifString1LessString2
- > 0-ifString1GreaterString2
|
PHP version: |
4 + |
Instance
Differences between natural algorithms (strnatcmp) and conventional computer string sorting algorithms (strcmp:
"; Usort ($ arr1," strcmp "); print_r ($ arr1); echo"
"; Echo" natural sequence string comparison "."
"; Usort ($ arr2," strnatcmp "); print_r ($ arr2);?>
Online operation