HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
PHP String Reference : Compare two strings using a "natural" algorithm (case-sensitive)
The strnatcmp() function compares two strings using a "natural" algorithm.
In a natural algorithm, 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.
Parameter | Description |
---|---|
string1 | Required. Specifies the first string to compare |
string2 | Required. Specifies the second string to compare |
Return Value: | This function returns:
|
---|---|
PHP Version: | 4+ |
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);
?>
❮ PHP String Reference