PHP strcmp () function usage instructions

Source: Internet
Author: User
Tags strcmp
The Strcmp () function compares a binary security to two strings and is case-sensitive.

Compare two strings in a case-sensitive manner
The Strcmp () function compares a binary security to two strings and is case-sensitive. The form is:
int strcmp (string str1, String str2)
Depending on the result of the comparison, one of the following possible values will be returned.
• Returns 0 if str1 and str2 are equal.
• Returns-1 if str1 is less than str2.
• Returns 1 if STR1 is greater than str2.
The site often asks the user for registration to enter and confirm the password he chooses, reducing the likelihood of incorrect password generation due to typos. Because passwords are usually case-sensitive, strcmp () is appropriate for comparing the two passwords:

The code is as follows:

<?php $pswd = "Supersecret"; $PSWD 2 = "Supersecret"; if (strcmp ($PSWD, $pswd 2)! = 0) echo "Your passwords do not match!"; else echo "Passwords match!"; ?>

Note that for strcmp (), the strings must match exactly to be considered equal. For example, Supersecret differs from Supersecret. If you want to compare two strings in a case-insensitive manner, consider the strcasecmp () described below.
Another area of confusion about this function is that it returns 0 when two strings are equal. This is different from using the = = operator to complete a string comparison, as follows:
if ($str 1 = = $str 2)
The two approaches target the same, comparing two strings, but keep in mind that they return different values.

Instance code:

The code is as follows:

<?php echo strcmp ("Hello world!", "Hello world!"); Returns 0?>

The following is a good example of the strcmp code:
PHP strcmp Control access code based on IP address

a simple addition:
The comparison between STR1 and str2 is actually the ASCII value of STR1 and str2.
Like what:
strcmp ("A", "a"); The return value is-1
The ASCII value of a is 65.
This example can also be seen when comparing strings with strcmp (), which is case-sensitive

Then look at strcmp's deep understanding:

STRCMP ("abc", "abc"); At this point, the string equals return value is 0
Let's change strcmp ("abc", "abc"); it's not equal at this point. The return value is-1
Since strcmp is to compare ABC and ABC one by one, the first and first comparison of two strings,

Two and a second comparison ... When the ASCII values are equal for each comparison, you can continue to compare the next pair

Character. Therefore, comparing the second pair B and B, is not equal, then the comparison stops, and the return value appears.

if ("abc" > "abc") Comparison principle

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.