Home  >  Article  >  Backend Development  >  Detailed explanation of the use of PHP string-based comparison functions strcmp() and strcasecmp()_PHP tutorial

Detailed explanation of the use of PHP string-based comparison functions strcmp() and strcasecmp()_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:10:11864browse

Use "===" to judge. As for the difference between it and "==", simply put, the former emphasizes the "identical (identical, exactly the same)" type and requires the same; the latter requires "equal (equal) )", the values ​​​​are the same. Or use strcmp to judge, but this can tell you whether the two strings are equal, but it cannot tell you where they are different.

Generally you can use !=, == to compare whether two objects are equal. The reason why they are two objects is because they are not necessarily all strings, they can also be integers, etc. For example

Copy code The code is as follows:

$a = "joe";
$ b = "jerry";
if ($a != $b)
{
echo "not equal";
}
else
{
echo "equal" ;
}

If you use !== , === to compare, the types of the two objects must be strictly equal to return true; otherwise, use ==,!=, the string will be Automatically convert to the corresponding type for comparison.
Copy code The code is as follows:

22 == "22"; // Return true
22 === "22"; // Return falsePHP functions used for string comparison: strcmp(), strcasecmp(), strncasecmp(), strncmp(), they are all if the former is greater than the latter If the former is smaller than the latter, an integer less than 0 is returned; if the two are equal, 0 is returned.

1)strcmp is used For case-sensitive (i.e. case-sensitive) string comparison:
2)echo strcmp("abcdd", "abcde"); // Returns 1 (>0), compared is "b" and "b"
3)strcasecmp is used for case-insensitive string comparison:
4)echo strcasecmp("abcdd", " abcde"); // Returns -1 (<0), what is compared is "d" and "e"
strncmp is used to compare part of the string, starting from the beginning of the string, the third parameter, For the length to be compared:
echo strncmp("abcdd", "abcde", 3); // Returns 1 (>0), comparing abc and abc
strncasecmp is used for case-insensitive comparison Part of the string, starting from the beginning of the string, the third parameter is the length to be compared:
echo strncasecmp("abcdd", "abcde", 3); // Returns 0, comparing abc and abc, since it is not case sensitive, the two are the same.
Another situation is that simply comparing the string sizes cannot meet our predetermined needs. For example, as usual, 10.gif will be larger than 5.gif, but if the above functions are applied, -1 will be returned, that is Indicates that 10.gif is better than 5.gif. For this situation, PHP provides two natural contrast functions strnatcmp and strnatcasecmp:
echo strnatcmp("10.gif", "5.gif"); // Return 1 (>0)
echo strnatcasecmp("10.gif", "5.gif"); // returns 1 (>0)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327133.htmlTechArticleUse "===" to judge. As for the difference between it and "==", it is simply the former. Emphasize that the "identical (identical, exactly the same)" type also requires the same; the latter requires "equal (equal...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn