PHP string comparison function_PHP tutorial

WBOY
Release: 2016-07-20 11:01:53
Original
886 people have browsed it

The most common way to compare whether two strings are equal is to use "===" to judge. As for the difference between it and "==", simply put, the former emphasizes that the "identical" type also requires the same; the latter If the user requires "equal", the same value is enough, refer to [1]. 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. My idea is to divide a single string into letters (characters), so that the comparison can accurately know where the difference is. To separate strings, just use "str_split". For syntax, refer to [2]. Then output the result array. The advantage is that even spaces will be used as elements of the array. My previous example was because the first string contained 2 spaces, while the latter one only had one. But the display you see when outputting is the same. You can also split according to other delimiters, such as "explode" or "preg_split",

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

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

If you use !==,=== (you can see an extra equal sign) for comparison, 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.

22 == "22"; // returns true
22 === "22"; // return false

Because of this, some unexpected "accidents" often occur in our programs:

0 == "I love you"; // returns true
1 == "1 I love you";//return true

There is a set of functions for string comparison in the PHP tutorial: strcmp, strcasecmp, strncasecmp(), strncmp(). If the former is greater than the latter, they will return an integer greater than 0; if the former If it is smaller than the latter, an integer less than 0 is returned; if the two are equal, 0 is returned. The principles of their comparison are the same as the rules of other languages.

strcmp is for case-sensitive (i.e. case-sensitive) string comparison:
echo strcmp("abcdd", "abcde"); // Returns 1 (>0), comparing "b" and "b"

strcasecmp for case-insensitive string comparison:
echo strcasecmp("abcdd", "abcde"); // Returns -1 (<0), comparing "d" and "e"

strncmp is used to compare part of a string, starting from the beginning of the string. The third parameter is the length to be compared:
echo strncmp("abcdd", "abcde", 3); // returns 1 (>0), comparing abc and abc

strncasecmp is used to compare part of a string in a case-insensitive manner, 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, which are the same since they are not case-sensitive.

Another situation is that simply comparing the string sizes cannot meet our predetermined needs. For example, normally 10.gif will be larger than 5.gif, but if you apply the above functions, -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"); // returns 1 (>0)
echo strnatcasecmp("10.gif", "5.gif"); // Return 1 (>0)


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445405.htmlTechArticleTo compare whether two strings are equal, the most common way is to use === to judge. As for it and = The difference between =, simply put, is that the former emphasizes the identical type and requires the same; the latter requires...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template