How to use string comparison functions in php-PHP source code

Source: Internet
Author: User
In the following section, we can know that character string comparison can be used in addition to function comparison, or character comparison. The following article will summarize the character string comparison issues. In the following article, we can know that character string comparison can be used in addition to comparing using functions, and character comparison can also be performed using ==or ===, the following is a summary of the string comparison issues.

Script ec (2); script

Comparing strings is one of the important features of string processing in any programming language. In PHP, besides the comparison operator (=, <或> In addition to comparison, a series of comparison functions are provided to allow PHP to perform more complex string comparison. Such as strcmp (), strcasecmp (), and strnatcmp.

1. String comparison in byte order

To compare strings in byte order, you can use the strcmp () and strcasecmp () functions. The strcasecmp () function can ignore the uppercase and lowercase letters in the string for comparison. The two functions are prototype as follows:

The Code is as follows:

In strcmp (string str1, string str2) // compares uppercase and lowercase letters in a string.
Int strcasecmp (string str1, string str2) // ignore the case-insensitive comparison of letters in the string

The usage of these two functions is similar. You need to input two string parameters for comparison. The input str1 and str2 strings can be compared from the first byte of the two strings according to the ASCII value of the byte. If they are equal, the next byte is compared until the comparison is completed. Returns one of the following values:
★If str1 is equal to str2, 0 is returned.
★If str1 is greater than str2, 1 is returned.
★If str1 is smaller than str2,-1 is returned.

In the following program, we can determine the size of two comparative strings by comparing the returned values. Use the strcmp () function to distinguish between uppercase and lowercase letters in a string. Use the strcasecmp () function to ignore the comparison of uppercase and lowercase letters in a string. Of course there is no practical significance. The Code is as follows:

The Code is as follows:
$ Username = "Admin ";
$ Password = "lampBrother ";

// Case-insensitive comparison. If two strings are equal, 0 is returned.
If (strcasecmp ($ userName, "admin") = 0 ){
Echo "user name exists ";
}
// Convert the functions corresponding to the two strings into uppercase or lowercase letters, or perform case-insensitive comparison.
If (strcasecmp (strtolower ($ userName), strtolower ("admin") = 0 ){
Echo "user name exists ";
}

// Compare the case sensitivity of letters in a string
Switch (strcmp ($ password, "lampbrother ")){
Case 0:
Echo "two strings are equal
"; Break;
Case 1:
Echo "the first string is greater than the second string
"; Break;
Case-1:
Echo "the first string is smaller than the second string
"; Break;
}
?>

2. String comparison by natural sorting

In addition to the dictionary order of bytes, PHP also provides the "natural sorting" method to compare strings. The so-called natural sorting refers to sorting by People's Daily thinking habits, that is, comparing the numbers in the string by the number size. For example, when comparing byte values, "4" is greater than "33", because "4" is greater than the first character in "33", and "33" is greater than "4" according to the natural sorting rule ". Use the strnatcmp () function to compare two strings by natural sorting. This function is case sensitive and uses a similar format as the strcmp () function.

In the following example, the names of files with numbers in an array are sorted by the bubble sort method in two comparison methods. The Code is as follows:

The Code is as follows:
// Define an array containing numeric values
$ Files = array ("file11.txt", "file22.txt", "file1.txt", "file2.txt ");

Function mySort ($ arr, $ select = false ){
For ($ I = 0; $ I For ($ j; $ j // If the second parameter is true, use the strcmp () function to compare the size.
If ($ select ){
// If the comparison result of the two values is greater than 0, the switch position
If (strcmp ($ arr [$ j], $ arr [j + 1])> 0 ){
$ Tmp = $ arr [$ j];
$ Arr [$ j] = $ arr [$ j + 1];
$ Arr [$ j + 1] = $ tmp;
}
// If the second parameter is false, use the strnatcmp () function to compare the size.
} Else {
// If the comparison result is greater than 0
If (strnatcmp ($ arr [$ j], $ arr [$ j + 1])> 0 ){
$ Tmp = $ arr [$ j];
$ Arr [$ j] = $ arr [$ j + 1];
$ Arr [$ j + 1]; = $ tmp;
}
}
}
}
Return $ arr; // sorted Array
}
Print_r (mySort ($ files, true); // sort by dictionary: file1.txt file11.txt file2.txt file22.txt
Print_r (mySort ($ files, false); // You can select file1.txt file2.txt file11.txt file22.txt in the natural order.
?>

In PHP, The strnatcasecmp () function that ignores case-insensitive versions of this function is also used in the same way as the strnatcmp () function.

In addition to the above function comparison, we can also use "=" to determine the difference between it and "= ".

Generally used! =, = Compares whether two objects are equal. Therefore, they are two objects because they are not all strings and can also be integers.
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.

The Code is as follows:
22 = "22"; // return true
22 = "22"; // return false

 

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

The Code is as follows:

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

Another comparison method

Var_dump ("1" = "1e0 ");
Var_dump ("1" = "0x1 ");
Var_dump ("20" = "2e1 ");
Var_dump ("10" = "0x0a ");

And so on;
Yes, maybe you will say that there is a summary of this issue in the appendix of the PHP manual. The question is, this is just to know, so why?
Today, a netizen asked me a question:

The Code is as follows:


Var_dump ("1" = "1e0"); // true
Var_dump ("1" = "1ef"); // false

Why are the two results different.
That is to say, the above equation a should be established, so the only explanation is that PHP says that both of them are compared as int.
However, var_dump ("1e0") or var_dump ("1") are all strings.

Because PHP does not differentiate data types, she adopts a strategy. When your variable looks like a number, she thinks it is a number.
In short, numeric_string is a string that represents numbers:
That is to say, the following comparison results are true:

The Code is as follows:

Var_dump ("1" = "1e0 ");
Var_dump ("1" = "0x1 ");
Var_dump ("20" = "2e1 ");
Var_dump ("10" = "0x0a ");

Have readers ever encountered this problem?

The Code is as follows:
If ("608E-4234" = "272E-3063 "){
Echo "oh shit! They're equal! \ N ";
} Else {
Echo "obviusly, isn' t? \ N ";
}

PHP Tips: To determine whether two strings are equal, use = instead of =.

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.