Usage of strnatcmp function in php: [strnatcmp($string1,$string2)]. The strnatcmp function compares two strings (case-sensitive) by using a "natural algorithm".
strnatcmp() is a built-in function of PHP that uses the "natural order" algorithm to compare two strings and return a positive integer, a negative number, or zero; case sensitive. The syntax is strnatcmp($string1,$string2), which accepts two required string parameters for comparison.
(Recommended tutorial: php video tutorial)
php How to use the strnatcmp() function?
php The strnatcmp function is used to compare two strings in a "natural order" algorithm, case-sensitive.
Basic syntax:
strnatcmp($string1,$string2)
Parameters: strnatcmp() function accepts two required string parameters for comparison
●$string1: required parameter, specifies the first string to be compared.
● $string2: Required parameter, specifies the second string to be compared.
Return value: The strnatcmp() function returns an integer value based on the following conditions:
● If the two strings are equal, the function returns 0.
● If $string1 is less than $string2, the function returns a negative value (<0).
● If $string2 is less than $string1, the function returns a positive value (> 0).
Explanation: In the 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.
Let’s take a look at how to use the php strnatcmp() function through an example.
<?php //使用自然算法来比较两个字符串 echo strnatcmp("2hello world!","01hello world!"); echo "<br>"; echo strnatcmp("01hello world!","2hello world!"); ?>
Output:
1 -1
The above is the detailed content of How to use strnatcmp function in php. For more information, please follow other related articles on the PHP Chinese website!