Home >>PHP String Functions >PHP strnatcmp() Function
PHP strnatcmp() function is used to compare two given strings using the "natural order" algorithm. It accepts two strings as input parameter and returns an integer value as output. It is similar to strnatcasecmp() but the only difference is strnatcmp() function is case-sensitive.
Syntax:
strnatcmp( $string1, $string2);
Parameter | Description |
---|---|
string1 | This is a required parameter. This parameter defines the first string to compare. |
string2 | This is a required parameter. This parameter defines the second string to compare. |
Here is an example of strnatcmp() function in PHP:
<html> <body> <?php echo strnatcmp("PHPTPOINT","PHP"); // case sensitive echo "<br>"; echo strnatcmp("PHPPOINT","PHPTPOINT"); echo "<br>"; echo strnatcmp("PHPTPOINT","PHPTPOINT"); ?> </body> </html>
Here is an another example of strnatcmp() function in PHP:
<html> <body> <pre> <?php $arr1 = $arr2 = array("1","2","10","001","1000","20","130","200"); echo "Standard string comparison"."<br>"; usort($arr1,"strcmp"); print_r($arr1); echo "<br>"; echo "<br>"; echo "Natural order string comparison"."<br>"; usort($arr2,"strnatcmp"); print_r($arr2); ?> </pre> </body> </html>