Instructions for using php strcmp() function

怪我咯
Release: 2023-03-12 21:30:01
Original
2113 people have browsed it

The

Strcmp() function performs a binary-safe comparison of two strings and is case-sensitive.

Compare two strings in a case-sensitive manner
The Strcmp() function performs a binary-safe comparison of two strings and is case-sensitive. 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.
•If str1 is greater than str2, return 1.
Websites often require users to be registered to enter and confirm the password they choose, reducing the possibility of generating incorrect passwords due to typing errors. Because passwords are usually case-sensitive, strcmp() is very suitable for comparing these two passwords:

The code is as follows:

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

Note that for strcmp (), Strings must match exactly to be 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)
The two methods have the same goal, Both compare two strings, but remember that the values ​​they return are different.

Example code:

The code is as follows:

<?php 
echo strcmp("Hello world!","Hello world!"); 
//返回0 
?>
Copy after login

The following is a better strcmp example code:
PHP strcmp code to control access based on IP address

Simple addition:
The comparison between str1 and str2 here is actually 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
It can also be seen from this example When strcmp() is used to compare strings, it is case-sensitive.

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

strcmp("abc","abc"); At this time, the string The return value for equality is 0
Let’s change strcmp("aBc", "abc"); at this time, it is not equal and the return value is -1
Since strcmp compares aBc and abc one by one, two characters The first string is compared with the first one, and the

second string is compared with the second one... When the ASCII values ​​of each comparison are equal, the next pair of

characters can be compared. . 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

The above is the detailed content of Instructions for using php strcmp() function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!