PHP Compare two strings (case sensitive) function strcmp ()

Source: Internet
Author: User
Tags first string php compare strcmp

Instance

Comparison of two strings (case-sensitive):

<?phpecho strcmp ("Hello world!", "Hello world!");? >

Definition and usage

The strcmp () function compares two strings.

Note: the strcmp () function is binary safe and case-sensitive.

Tip: This function is similar to the strncmp () function, but by strncmp () you can specify the number of characters each string is used to compare.

Grammar

strcmp (STRING1,STRING2)
Parameters Describe
String1 Necessary. Specifies the first string to compare.
string2 Necessary. Specifies a second string to compare.

Technical details

return value: This function returns:
  • 0-if two strings are equal

  • <0-if string1 is less than string2

  • >0-if string1 is greater than string2

php version: 4+

More examples

Example 1

Compare two strings (case-sensitive, hello and hello output are not the same):

<?phpecho strcmp ("Hello", "Hello"), echo "<br>", Echo strcmp ("Hello", "Hello"); >

Example 2

Different return values:

<?phpecho strcmp ("Hello world!", "Hello world!"); The strings is Equalecho strcmp ("Hello world!", "Hello"); String1 is greater than String2echo strcmp ("Hello world!", "Hello world! Hello! "); String1 is less than string2?>

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:

<?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:

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

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.