Php strcmp instructions

Source: Internet
Author: User

Compare two strings in case-sensitive mode
The Strcmp () function compares two strings in binary mode and is case sensitive. The format is:
Int strcmp (string str1, string str2)
The following possible values are returned based on the comparison results.
• If str1 and str2 are equal, 0 is returned.
• If str1 is smaller than str2,-1 is returned.
• If str1 is greater than str2, 1 is returned.
Websites often require users to enter and confirm their selected passwords to reduce the possibility of generating incorrect passwords due to incorrect typing. Because the password is usually case sensitive, strcmp () is very suitable for comparing the two passwords:
Copy codeThe Code is as follows:
<? Php
$ Pswd = "supersecret ";
$ Pswd2 = "supersecret ";
If (strcmp ($ pswd, $ pswd2 )! = 0)
Echo "Your passwords do not match! ";
Else
Echo "Passwords match! ";
?>

Note: For strcmp (), strings must be exactly matched to be considered equal. For example, Supersecret is different from supersecret. To compare two strings in case-insensitive mode, consider the strcasecmp () introduced below ().
Another obfuscation of this function is that 0 is returned when two strings are equal. This is different from using the = Operator to complete a string, as shown below:
If ($ str1 = $ str2)
The two methods share the same objective, both of which compare two strings, but remember that they return different values.

Instance code:
Copy codeThe Code is as follows:
<? Php
Echo strcmp ("Hello world! "," Hello world! ");
// Return 0
?>

The following is a good example of strcmp code:
PHP strcmp IP address-Based Access Control Code


Simple addition:
The Comparison Between str1 and str2 is actually a comparison of the ASCII values of str1 and str2.
For example:
Strcmp ("A", "a"); Return Value:-1
// The ASCII value of a is 97. The ASCII value of A is 65.
This example also shows that when strcmp () is used to compare strings, It is case sensitive.

Next, let's look at the in-depth understanding of strcmp:

Strcmp ("abc", "abc"); returns 0 if the string is equal.
Let's change strcmp ("aBc", "abc"). In this case, the return value is-1.
Since strcmp compares aBc and abc one by one, the first and first comparisons of the two strings

Comparison between two and two ...... When the ASCII value of each comparison is equal, the next pair can be compared.

Character. Therefore, if the comparison between the second pair B and B is not equal, the comparison is stopped and the return value is displayed.

If ("abc"> "aBC") has the same principle of comparison.

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.