Use of PHP string-based comparison functions strcmp () and strcasecmp ()
Source: Internet
Author: User
This article provides a detailed analysis of the use of the comparison functions strcmp () and strcasecmp () of PHP strings. For more information, see "=, as for the difference between it and "=", simply put, the former emphasizes that the "identical (same, identical)" type also requires the same; the latter requires "equal (equal )", the value is the same. Or use strcmp to determine, but this can tell you whether the two strings are equal, but it cannot tell you where the difference is.
Generally used! =, = Compares whether two objects are equal. The reason is that they are two objects, because they are not all strings and can also be integers.
For example The code is as follows:
$ A = "joe ";
$ B = "jerry ";
If ($! = $ B)
{
Echo "not equal ";
}
Else
{
Echo "equal ";
}
If yes! =, = For comparison, if the two objects must be of the same type, true is returned; otherwise, = ,! = The string is automatically converted to the corresponding type for comparison.
The code is as follows:
22 = "22"; // return true
22 = "22"; // returns the functions used by falsePHP for string comparison: strcmp (), strcasecmp (), strncasecmp (), strncmp (), if the former is greater than the latter, an integer greater than 0 is returned. if the former is smaller than the latter, an integer smaller than 0 is returned. if the two are equal, 0 is returned.
1)Strcmp is a case-sensitive string comparison:
2)Echo strcmp ("abcdd", "abcde"); // returns 1 (> 0), comparing "B" and "B"
3)Strcasecmp is used for case-insensitive string comparison:
4)Echo strcasecmp ("abcdd", "abcde"); // returns-1 (<0), comparing "d" and "e"
Strncmp is used to compare a part of a string, starting from the beginning of the string, and the third parameter, which is the length to be compared:
Echo strncmp ("abcdd", "abcde", 3); // returns 1 (> 0), comparing abc and abc
Strncasecmp is used to compare a part of a case-insensitive string. it starts from the beginning of the string and the third parameter is the length to be compared:
Echo strncasecmp ("abcdd", "abcde", 3); // returns 0, comparing abc and abc. The two are the same because they are case insensitive.
Another scenario is to compare the size of a string to meet our predefined requirements. for example, 10.gif is larger than 5.gif as usual, but if the above functions are applied,-1 is returned, in this case, php provides two natural comparison functions, strnatcmp and strnatcasecmp:
Echo strnatcmp ("10.gif"," 5.gif"); // return 1 (> 0)
Echo strnatcasecmp ("10.gif"," 5.gif"); // return 1 (> 0)
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.