Home > Article > Backend Development > How to use strnatcmp function
PHP strnatcmp function: Compare strings using natural sorting algorithm.
php strnatcmp() function syntax
Function: compare strings in natural order
Syntax:
strnatcmp(string1,string2)
Parameters:
string1 Required, specifies the first string to be compared.
string2 must specify the second string to be compared.
Description: The strnatcmp() function uses a "natural" algorithm to compare two strings. In natural arithmetic, 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. Note that this function is case-sensitive. If the two strings are equal, 0 is returned. If string1 is greater than string2, the returned value is greater than 0. If string1 is less than string2, the returned value is less than 0.
php strnatcmp() function usage example:
<?php //使用自然算法来比较两个字符串 echo strnatcmp("2hello world!","01hello world!"); echo "<br>"; echo strnatcmp("01hello world!","2hello world!"); ?>
Output:
1 -1
This article is an introduction to the PHP strnatcmp function. I hope it will be helpful to friends in need!
The above is the detailed content of How to use strnatcmp function. For more information, please follow other related articles on the PHP Chinese website!