Home >Backend Development >PHP Tutorial >How to use strnatcasecmp function in php
Usage of strnatcasecmp function in php: [strnatcasecmp($string1, $string2)]. The strnatcasecmp function can compare two strings through a "natural algorithm".
The strnatcasecmp() function is a built-in function in PHP that uses the "natural order" algorithm to compare two strings (not case-sensitive). The syntax is strnatcasecmp($string1, $string2) accepts two strings as parameters and returns an integer value (positive, negative or zero).
(Recommended tutorial: php video tutorial)
php How to use the strnatcasecmp() function?
php The strnatcasecmp() function uses the "natural" algorithm to compare two strings (case-insensitive).
Note: The strnatcasecmp() function is similar to the strnatcmp() function. The only difference is that the function is not case-sensitive.
Basic syntax:
strnatcasecmp($string1,$string2)
Parameters: The function accepts two required string parameters, as shown in the syntax above. These parameters are defined as follows:
● $string1: This parameter specifies the first string to be compared and cannot be omitted.
● $string2: This parameter specifies the second string to be compared and cannot be omitted.
Return value: This function returns a positive integer, a negative number, or 0 depending on the following conditions:
● If the two strings are equal, it returns 0 if $string1 is greater than $ string2, a positive value (> 0) is returned.
●If $string1 is less than $string2, return a negative value (<0).
Let’s take a look at how to use the php strnatcasecmp() function through an example.
Example 1:
<?php echo strnatcasecmp("2hello world!","01hello world!"); echo "<br>"; echo strnatcasecmp("01hello world!","2hello world!"); ?>
输出: 1 -1
Example 2:
<?php echo strnatcasecmp("Hello", "HELLO"); echo "<br>"; echo strnatcmp("Hello", "HELLO"); ?>
Output:
0 1
The above is the detailed content of How to use strnatcasecmp function in php. For more information, please follow other related articles on the PHP Chinese website!