Home>

in phpThe strnatcmp () function uses a "natural" algorithm to compare two strings (case sensitive). Usually in natural algorithms,The number 2 is less than the number 10. In computer sorting,10 is less than 2 because the first number in 10 is less than 2.

The definition of the strnatcmp () function is 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 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 codeThe following:

<?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 "<br />";
 echo strcmp ($str3, $str4);//Compare by byte,Return 1
 echo "<br />";
 echo strnatcmp ($str1, $str2);//Compare by "natural order" method,Returns -1
 echo "<br />";
 echo strnatcmp ($str3, $str4);//Compare by "natural order" method,Returns 1
?>

The results are as follows:

1
1
-1
1
php
  • Previous angularjs create popup box to achieve drag effect
  • Next Detailed configuration of adding an authentication password for a website or directory in the Nginx server