php strcmp usage instructions_PHP tutorial

WBOY
Release: 2016-07-21 15:38:49
Original
859 people have browsed it

Compare two strings in a case-sensitive manner
The Strcmp() function performs a binary-safe comparison of two strings, case-sensitively. Its form is:
int strcmp ( string str1 , string str2 )
One of the following possible values ​​will be returned based on the comparison result.
•Return 0 if str1 and str2 are equal.
•If str1 is less than str2, return -1.
•Return 1 if str1 is greater than str2.
Websites often require the user to be registered to enter and confirm his chosen password, reducing the possibility of generating an incorrect password due to typing errors. Because passwords are usually case-sensitive, strcmp() is very suitable for comparing these two passwords:

Copy code The code is as follows:

$pswd = "supersecret";
$pswd2 = "supersecret";
if (strcmp($pswd,$pswd2) != 0)
echo "Your passwords do not match!";
else
echo "Passwords match!";
?>

Note that for strcmp ( ), the string must be completely Matches are considered equal. For example, Supersecret is different from supersecret. If you want to compare two strings in a case-insensitive manner, consider strcasecmp () described below.
Another confusing point about this function is that it returns 0 when two strings are equal. This is different from using the == operator to complete string comparison, as follows:
if ( $str1 == $str2)
Both methods have the same goal, they are comparing two strings, but remember , but the values ​​they return are different.

Example code:
Copy code The code is as follows:

echo strcmp("Hello world!","Hello world!");
//Return 0
?>

The following is better Example code of strcmp:
PHP strcmp code to control access based on IP address


Simple addition:
Str1 and str2 here are more practical The above is a comparison of the ASCII values ​​of str1 and str2
For example:
strcmp("A", "a"); The return value is -1
// The ASCII value of a is 97 The ASCII value of A is 65
From this example, we can also see that when strcmp() is used to compare strings, it is case-sensitive

Then let’s look at the in-depth understanding of strcmp:

strcmp("abc ","abc"); At this time, the return value of string equality is 0
Let's change strcmp("aBc", "abc"); At this time, it is not equal and the return value is -1
Since strcmp is Let aBc and abc be compared one by one, the first of the two strings is compared with the first, the second

is compared with the second... Only when the ASCII values ​​of each comparison are equal can we continue. Compare the next pair of

characters. Therefore, if the second pair B and b are compared and they are not equal, then the comparison stops and a return value appears.

if ("abc">"aBC") The comparison principle is the same

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321720.htmlTechArticleCompare two strings in a case-sensitive manner. The Strcmp() function performs a binary safe comparison of two strings. Comparisons are case-sensitive. Its form is: int strcmp ( string str1 , str...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template