Parameter instance
Use the "natural" algorithm to compare two strings (case-sensitive):
<?phpecho strnatcmp ("2Hello world!", "10Hello world!"); echo "<br>", Echo strnatcmp ("10Hello world!", "2Hello world!"); >
Definition and usage
The strnatcmp () function uses a "natural" algorithm to compare two strings (case-sensitive).
In natural algorithms, the number 2 is less than the number 10. In computer sequencing, 10 is less than 2, because the first number in 10 is less than 2.
Note: The function is case-sensitive.
Grammar
STRNATCMP (STRING1,STRING2)
Parameters |
Describe |
String1 |
Necessary. Specifies the first string to compare. |
string2 |
Necessary. Specifies a second string to compare. |
technical details
return value: |
This function returns:
-
0-if two strings are equal
-
<0-if string1 is less than string2
-
>0-if string1 is greater than string2
|
php version: |
4+ |
More examples
Example 1
The difference between the natural algorithm (STRNATCMP) and the regular computer string sorting algorithm (strcmp):
<?PHP$ARR1 = $arr 2 = Array ("Pic1", "Pic2", "PIC10", "pic01", "pic100", "Pic20", "Pic30", "pic200"); echo "Standard string Comparison "." <br> "; Usort ($arr 1," strcmp ");p Rint_r ($arr 1); echo" <br> "; echo" Natural order string comparison "." <br> "; Usort ($arr 2," strnatcmp ");p Rint_r ($arr 2);? >
Case
<?phpecho strnatcmp ("2Hello world!", "10Hello world!"); echo "<br/>"; Echo strnatcmp ("10Hello world!", "2Hello world!"); >
The output of the code above would be:
The above code will output the following result:-1 1