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

Comparison between string and number in PHP

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "the comparison of strings and numbers in PHP". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the comparison of strings and numbers in PHP".

Be careful! Comparison between string and number in PHP

In daily development, the = = operator is something we come into contact with every day. There are actually a lot of holes buried in this operator. today, let's take a look at the problems that need to be paid attention to when strings and numbers are used as =.

First, let's take a look at this code:

1echo'"1234" = "1234" is'. ('1234' = '1234'), PHP_EOL

2echo'"1234" = "\ n1234" is'. ('1234' = "\ n1234"), PHP_EOL

3echo'"1234" = "1234" is'. ('1234' = '1234'), PHP_EOL

4echo'"1234" = "1234" is'. ('1234' = '1234'), PHP_EOL

5echo'"1234" = "1234\ n" is'. ('1234' = "1234\ n"), PHP_EOL

They are all string = = operations. what will be their result?

1 "1234" = = "1234" is 1

2 "1234" = "\ n1234" is 1

3 "1234" = "1234" is 1

4 "1234" = "1234" is

5 "1234" = "1234\ n" is

Yes, the leading spaces or tabulation symbols ignore these symbols, that is, these strings are type-converted during comparison and are strongly converted to int. If the special character comes later, it will be compared according to the string type. what about the pure character type?

1echo'"aa" = = "aa" is'. ('aa' = =' aa'), PHP_EOL

2echo'"aa" = "\ naa" is'. ('a'= ='\ naa'), PHP_EOL

3echo'"aa" = = "aa" is'. ('aa' = =' aa'), PHP_EOL

4echo'"aa" = = "aa" is'. ('aa' = =' aa'), PHP_EOL

5echo'"aa" = = "aa\ n" is'. ('aa' = = "aa\ n"), PHP_EOL

At this point, the results are in line with our expectations, they are string comparisons themselves, and there is no conversion of any kind:

1 "aa" = = "aa" is

2 "aa" = "\ naa" is

3 "aa" = = "aa" is 1

4 "aa" = = "aa" is

5 "aa" = = "aa\ n" is

To sum up the experimental results, when the content of the string is int data, the = = comparison of the string will ignore the spaces or tabulation symbols that appear in front of the string and force them to int type. As long as a string contains text or a special symbol after a number, it is compared as text, such as plain text or mixed text ("11aa", "11\ n", "aa11").

Thank you for your reading, the above is the content of "comparison of strings and numbers in PHP". After the study of this article, I believe you have a deeper understanding of the comparison of strings and numbers in PHP, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report