PHP string comparison function

Source: Internet
Author: User

comparing two strings is equal, the most common method is to use "= = =" To judge, as to its difference with "= =", the former emphasizes the "identical" type also requires the same, the latter requires "equal", the value is the same , refer to "1". 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. My idea is that a single string is divided into letters (character) so that comparisons can be made to know exactly where they are. Delimited string, using "Str_split" on it, Syntax reference "2". Then output an array of results, the advantage is that even a space will be an element of the array. My previous example is because the previous string contains 2 spaces, and the second one is only one. But the output will see the same display. You can also split by other separators, such as "explode" or "preg_split",

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. Like what

The code is as follows Copy Code

$a = "Joe";
$b = "Jerry";
if ($a! = $b)
{
echo "Unequal";
}
Else
{
echo "equal";
}

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.

The code is as follows Copy Code
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)

PHP string comparison function

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.