PHP strcmp use instructions _php tips

Source: Internet
Author: User
Tags strcmp
Compares two strings in a case-sensitive way
The Strcmp () function compares binary security to two strings and is case-sensitive. In the form of:
int strcmp (string str1, String str2)
Depending on the result of the comparison, one of the following possible values is 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 website often asks the user to be registered to enter and confirm the password he chooses, reducing the likelihood of generating an incorrect password due to a typing error. Because passwords are usually case-sensitive, strcmp () is appropriate for comparing these two passwords:
Copy Code code as follows:

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

Note that for strcmp (), the string must be fully matched to be considered equal. For example, Supersecret is different from Supersecret. If you want to compare two strings in a case-insensitive manner, consider the strcasecmp () described below.
Another confusing point about this function is that two strings are equal to return 0. This is different than using the = = operator to complete the string comparison, as follows:
if ($str 1 = = $str 2)
The goal is the same in both ways, comparing two strings, but remember that they return different values.

Instance code:
Copy Code code as follows:

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

Here's a good example of strcmp code:
PHP strcmp Control access code based on IP address


easy to add:
The comparison between the 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 also shows that when you compare strings with strcmp (), they are case-sensitive

And then look at strcmp's deep understanding:

STRCMP ("abc", "abc"); At this point the string equality return value is 0
Let's Change the strcmp ("ABC", "abc"); This is not equal. The return value is-1
Since strcmp is a comparison between ABC and ABC, the first and first comparisons of the two strings, section

Two and a second comparison ... You can continue to compare the next pair when the ASCII values for each comparison are equal

Character. So, compared to the second pair B and B, not equal, then the comparison stops, 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.