string comparison description
When S1<S2, returns a negative number
When s1=s2, return value = 0
Returns a positive number when S1>S2
1<?PHP2 $str 1= ' str100 ';3 $str 2= ' Str20 ';4 Echo strcmp($str 1,$str 2)." <br> ";5 //Output-16 Echo strnatcmp($str 1,$str 2)." <br> ";7 //Output18 Echo strncmp($str 1,$str 2, 5). " <br> ";9 //Output-1Ten One?>
The strcmp (str1, str2) function compares two strings by character, that is, two strings are compared from left to right by character (compared to the ASCII value size) until a different character occurs or a '/s ' is encountered. such as: "A" < "B" "a" > "a" "Computer" > "Compare"
The strnatcmp (str1, str2) function uses a natural sorting algorithm to compare strings This function implements a comparison algorithm that is used to sort numeric strings by human habit, which is the "natural order".
The principle of strncmp (str1, str2, len) function comparison refers to the strcmp (str1, str2) function, except that only the Len characters at the beginning of the string are compared.
PS: The above three functions are case-sensitive, if the comparison of the string is not case-sensitive, there are another three similar functions: strcasecmp,strnatcasecmp,strncasecmp.
The difference between PHP strcmp,strnatcmp,strncmp functions