strnatcmp()是PHP上的内置函数。此函数使用“natural order”算法比较两个字符串,并返回正整数,负数或零。此功能区分大小写。
用法:
strnatcmp( $string1, $string2 )
参数:该函数接受两个强制字符串参数进行比较,如上面的语法所示。
$string1:此参数指定要比较的第一个字符串。
$string 2:此参数指定要比较的第一个字符串。
返回值:该函数根据以下条件返回整数值:
如果两个字符串相等,则该函数返回0。
函数返回负值(
如果$string2小于$string1,则函数返回正值(> 0)。
例子:
Input : $string1 = "Hello", $string2 = "HEllo"
Output : 1
Input : $string1 = "Geek", $string2 = "Geeks"
Output : -1
以下示例程序旨在说明PHP中的strnatcmp()函数:
程序1:该程序显示了strnatcmp()函数的简单用法。
echo strnatcmp("Geek", "Geeks");
?>
输出:
-1
程序2:该程序显示strnatcmp()函数的区分大小写。
echo strnatcmp("Geeks", "GEEks");
?>
输出:
1
程序3:此程序说明了strcmp()和strnatcmp()函数之间的区别。
echo strnatcmp("Geek of month 2", "Geek of month 10");
echo "\n";
echo strcmp("Geek of month 2", "Geek of month 10");
?>
输出:
-1
256
Explanation :In a natural algorithm, the number 2 is less than the number 10 whereas in computer sorting, 10 is considered to be less than 2 as the first number in “10” is less than 2.