what is the syntax of the STRNATCMP() function in php?
strnatcmp(string1,string2)
Parameter | Description |
---|---|
string1 | Required. Specifies the first string to compare |
string2 | Required. Specifies the second string to compare |
examples of the STRNATCMP() function
Example 1. In this example, we compare two strings using a “natural” algorithm (case-sensitive).
<?php
echo strnatcmp("2Hello world!","10Hello world!");
echo "<br>";
echo strnatcmp("10Hello world!","2Hello world!");
?>
Example 2. In this example, we find difference between natural algorithm (strnatcmp) and regular computer string sorting algorithms (strcmp).
<?php
$arr1 = $arr2 = array("pic1","pic2","pic10","pic01","pic100","pic20","pic30","pic200");
echo "Standard string comparison"."<br>";
usort($arr1,"strcmp");
print_r($arr1);
echo "<br>";
echo "Natural order string comparison"."<br>";
usort($arr2,"strnatcmp");
print_r($arr2);
?>