PHP string-based comparison function strcmp () and strcasecmp () _php Tutorial

Source: Internet
Author: User
Use "= = =" To judge, as to the difference between it and "= =", in short, the former emphasizes that the "identical (same, identical)" type is also required, the latter requires "equal (equal)", the value of the same is OK. 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.

You can generally use! =, = = Compare two objects for equality, two objects because they are not necessarily all strings, they can be integers, and so on. like
Copy CodeThe code is as follows:
$a = "Joe";
$b = "Jerry";
if ($a! = $b)
{
echo "Unequal";
}
Else
{
echo "equal";
}

If you use!==, = = =, two object types are strictly equal to return true, otherwise ==,!= will automatically convert the string to the appropriate type for comparison.
Copy CodeThe code is as follows:
22 = = "22"; Returns True
22 = = = "22"; Returns falsephp functions for string comparisons: strcmp (), strcasecmp (), strncasecmp (), strncmp (), all of them if the former is larger than the latter, 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.

1)STRCMP is a string comparison that is used for case sensitivity (that is, case sensitive):
2)Echo strcmp ("ABCDD", "ABCDE"); Returns 1 (>0), comparing "B" and "B"
3)STRCASECMP for case-insensitive string comparisons:
4)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://www.bkjia.com/PHPjc/327133.html www.bkjia.com true http://www.bkjia.com/PHPjc/327133.html techarticle use "= = =" To judge, as to the difference between it and "= =", in short, the former emphasizes "identical (identical, identical)" type is also required, the latter requires "equal" (Equal ...

  • Related Article

    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.