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 compare whether strings are equal in php

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to compare strings in PHP. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

Php compares strings for equality by: 1, using the "string1 == string2" statement;2, using the "string1 == string2" statement;3, using the "strcmp(string1, string2)" statement;4, using the "strcasecmp(string1, string2)" statement, and so on.

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

Compare two strings for equality

It can usually be used!=,== Comparing two objects for equality is only called two objects because they are not necessarily all strings, they can also be integers, and so on. such as

$a = "joe";$b = "jerry";if ($a != $b){ echo "not equal";}else{ echo "equal";}

If you use!==,=== (You can see one more equal sign) If you compare, the types of the two objects must be strictly equal to return true; otherwise, use ==,!= The string is automatically converted to the appropriate type for comparison purposes.

22 == "22"; //returns true22 === "22"; //returns false

Because of this, our programs often have unexpected "accidents":

0 == "I love you"; //returns true1 == "1 I love you";//returns true

php tutorial has such a set of functions for string comparison: strcmp, strcasecmp, strncasecmp(), strncmp(), return value description:

strcmp is a string comparison used for case-sensitive (i.e., case-sensitive):

echo strcmp("abcdd", "abcde"); //returns 1 (>0), compares "b" and "b"

strcasecmp for case-insensitive string comparisons:

echo strcasecmp("abcdd", "abcde"); //returns-1 (0), compares abc and abc

strncasecmp is used for case-insensitive comparison of a portion of a string, starting at the beginning of the string, and the third argument is the length to be compared:

echo strncasecmp("abcdd", "abcde", 3); //returns 0, compares abc and abc, which are the same because they are case-insensitive.

There is also a case where comparing string sizes alone does not meet our predetermined requirements. For example, 10.gif will be larger than 5.gif according to common sense, but if the above functions are applied, it will return-1, which means that 10.gif is larger than 5.gif. For this case, php provides two natural contrast functions strnatcmp,strnatcasecmp:

echo strnatcmp("10.gif", "5.gif"); //returns 1 (>0)echo strnatcasecmp("10.gif", "5.gif"); //returns 1 (>0)

Thank you for reading! About "how to compare strings in php is equal" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

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

*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