PHP string comparison

Source: Internet
Author: User

The most common way to compare the equality of two strings is to use "= = =" To determine the difference between it and "= =", in short, the former emphasizes the "identical" type also requires the same, the latter requires "equal", the value of the same can be. Or use strcmp to judge, but this can tell you whether the two strings are equal, but can't tell you where the difference is.

Generally can use! =, = = Compare two objects are equal, only so is two objects, because they are not necessarily all strings, can be integer and so on.
If you compare with!==,=== (you can see an equal sign), the two object types are strictly equal to return true, otherwise ==,!= will automatically convert the string to the appropriate type for comparison.

22 = = "22"; Returns True
22 = = = "22"; Returns false

Because of this, there are often unexpected "surprises" in our programs:

0 = = "I love You"; Returns True
1 = = "1 I love You";//Returns True

The PHP tutorial also has a set of functions for string comparisons: strcmp,strcasecmp,strncasecmp (), strncmp (), all of them if the former is larger than the latter, and returns an integer greater than 0, or an integer less than 0 if the former is smaller than the latter. If the two are equal, 0 is returned. The principles they compare are the same as the rules of other languages.

STRCMP is a string comparison that is used for case sensitivity (that is, case sensitive):
Echo strcmp ("ABCDD", "ABCDE"); Returns 1 (>0), comparing "B" and "B"

STRCASECMP for case-insensitive string comparisons:
Echo strcasecmp ("ABCDD", "ABCDE"); Returns-1 (<0), compared with "D" and "E"

STRNCMP is used to compare a portion of a string, starting from the beginning of the string, and the third argument, the length to compare:
echo strncmp ("ABCDD", "ABCDE", 3); Returns 1 (>0), comparing ABC and ABC

STRNCASECMP is used as part of a case-insensitive comparison string, starting from the beginning of the string, and the third parameter, which is the length to compare:
Echo strncasecmp ("ABCDD", "ABCDE", 3); Returns 0, comparing ABC and ABC, because it is not case-sensitive, so the two are the same.

One more case is to compare the string size alone, not up to our predetermined requirements, such as normal 10.gif will be larger than 5.gif, but if the above functions, will return 1, that is, 10.gif than 5.gif, for this case, PHP provides two natural contrast function strnatcmp,strnatcasecmp:

Echo strnatcmp ("10.gif", "5.gif"); Returns 1 (>0)
Echo strnatcasecmp ("10.gif", "5.gif"); Returns 1 (>0)

Http://hi.baidu.com/19821213/blog/item/14192c2aaeccfc315343c180.html

in PHP, the use of = = is unreliable. Even if the variables are of the same type, = = True does not mean that they are equal. So in PHP try to use = = = to compare , use = = before you must think twice . in php = = = operation is zend_is_identical,php will not try to change the value of the type, very rigorous, the speed of comparison is also faster. For example:

$AA = ' 88. '; /Multiple Points
$b = ' 88 ';

if ($aa = = $b)

echo "equal";

the resulting results are equal. Since 88 is a number, PHP will automatically convert to integer or float for = = comparison, of course, it will be equal; strcmp is cast to a string type comparison, = = = is required to be the same value and the Type 88 is the int type, 88. is a floating-point type, of course not. . Using identity = = = does not result in an equal conclusion. Using strcmp () does not get the same conclusion. It is best not to use = = as a string equality comparison. Switch to strcmp ();

For both strings, you will first determine if they are numeric_string, and if so, they will be converted into plastic to compare. So what is numeric string? Because PHP does not differentiate between types, it takes a strategy that when your variable looks like a number, then she thinks the variable is a number and then converts it to a shape to compare.

PHP string comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.