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