Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to judge that two strings equal to not equal in php

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to judge two strings equal to not equal" in php. In daily operation, I believe that many people have doubts about how to judge two strings equal to not equal in php. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to judge two strings equal to not equal" in php. Next, please follow the editor to study!

Judgment method: 1, use the "= =" operator, syntax "string 1 string 2"; 2, use strcmp () function, syntax "strcmp (string 1, string 2)"; 3, use strcasecmp () function, syntax "strcasecmp (string 1, string 2)".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In php, it is judged that the string and so on is not equal to (equal)

Method 1: use the "=" operator

"= =" is the congruent operator, and returns TRUE if not only the values of $an and $b are equal, but also the type of their values. Otherwise, it returns FALSE.

Method 2: use the strcmp () function

The strcmp () function makes a binary-safe comparison of two strings and is case-sensitive. The syntax format is as follows:

Strcmp (string $str1, string $str2)

Where $str1 and $str2 are the two strings to compare, respectively.

Depending on the result of the comparison, the strcmp () function returns a different value. If $str1 is less than $str2, it returns a value.

< 0;如果 $str1 大于 $str2,则返回值 >

0; returns 0 if $str1 is equal to $str2.

When we register an account on a website, we usually need to enter the user's password twice to confirm it, so as to reduce the password setting errors caused by typing errors. Because passwords are usually case-sensitive, you can use the strcmp () function to compare passwords entered twice, as shown in the sample code:

Note that for the strcmp () function, the two strings to be compared must match exactly to be considered equal. For example, $pwd1 and $pwd2 in the sample code are different. You can use the strcasecmp () function if you want to compare two strings in a case-insensitive manner.

Method 3: use the strcasecmp () function

The strcasecmp () function in PHP is similar to the strcmp () function in that it can compare two strings, except that the strcasecmp () function is not case-sensitive when comparing strings, and its syntax format is as follows:

Strcasecmp (string $str1, string $str2)

Where $str1 and $str2 are the two strings to compare, respectively.

Based on the comparison result, if $str1 is less than $str2, a value is returned

< 0;如果 $str1 大于 $str2,则返回值 >

0; returns 0 if $str1 is equal to $str2.

The URLs of the website are case-insensitive, so we can use the strcasecmp () function to compare whether the two URLs are the same. The sample code is as follows:

At this point, the study of "how to judge that two strings are not equal in php" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 224

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report