PHP strnatcmp() Function

P
PHP strnatcasecmp() Function
PHP strncasecmp() Function

what is the syntax of the STRNATCMP() function in php?

strnatcmp(string1,string2)
ParameterDescription
string1Required. Specifies the first string to compare
string2Required. Specifies the second string to compare
PHP STRNATCMP() method

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);
?>
PHP strnatcasecmp() Function
PHP strncasecmp() Function

Add comment

Tutor Network

Share this

Learn PHP from A to Z