Php string comparison function _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php string comparison function. The most common method to compare whether two strings are equal is to use it for determination. The difference is that the former emphasizes the same identical type; the latter requires comparing two strings to see if they are equal. The most common method is to use "=" to determine whether they are equal. As for the difference between it and "=, simply put, the former emphasizes that the "identical" type also requires the same; the latter requires "equal", and the value is the same. refer to [1 ]. Or use strcmp to determine, but this can tell you whether the two strings are equal, but it cannot tell you where they are different. My idea is to split a single string into letters (character), so that you can know exactly where it is. Use "str_split" to separate strings. for the syntax, refer to [2 ]. Then output the result array. the advantage is that even spaces are used as elements of the array. In my previous example, the previous string contains two spaces, and the next string has only one space. However, the output displays the same information. It can also be split by other separators, such as "explode" or "preg_split ",

Generally used! =, = Compares whether two objects are equal. Therefore, they are two objects because they are not all strings and can also be integers. For example

$ A = "joe ";
$ B = "jerry ";
If ($! = $ B)
{
Echo "not equal ";
}
Else
{
Echo "equal ";
}

If yes! =, = (An equal sign is displayed). if the two objects are compared, the return value is true only when they are of the same type. Otherwise, the return value is = ,! = The string is automatically converted to the corresponding type for comparison.

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

Because of this, we often encounter unexpected "accidents" in our programs ":

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

In the php Tutorial, there is a set of functions used for string comparison: strcmp, strcasecmp, strncasecmp (), strncmp (). If the former is larger than the latter, return an integer greater than 0. if the former is smaller than the latter, return an integer smaller than 0. if the two are equal, return 0. their comparison principles are the same as those of other languages.

Strcmp is a case-sensitive string comparison:
Echo strcmp ("abcdd", "abcde"); // returns 1 (> 0), comparing "B" and "B"

Strcasecmp is used for case-insensitive string comparison:
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)


...

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.