Home >Backend Development >PHP Tutorial >Comparison of numbers and strings in PHP

Comparison of numbers and strings in PHP

不言
不言Original
2018-04-16 16:09:123054browse

The content of this article is about the comparison of numbers and strings in PHP. It has a certain reference value. Now I share it with you. Friends in need can refer to it

Comparison operations in PHP The symbols are a bit weird and easy to make mistakes. Here are the comparison rules:

1. When two characters are compared in size, the ASCII code size of the two characters is compared - this is easy to understand.

2. When two strings are compared in size, they start from the first character and compare the corresponding ASCII sizes. As long as they start from a certain corresponding position, the current position of one of the strings is If the character is larger than the corresponding character in another string, the size of the two strings can be directly determined, such as 'ba'>'az' - everyone knows this.

Then comparing '10' and 'a', of course it is still the same. First compare '1' and 'a' ASCII code, 'a' is larger.

3. When a number is compared with a string/character, the system first tries to convert the string/character into an integer/floating point type, and then compares it. For example, '12bsd' is converted to 12. 'a' is converted to 0. It must be noted that at this time, the corresponding ASCII code value is not compared with the number.

In fact, for the same reason, the result of 'a' 10 is also 10.

And it is easy to ignore: 0 is compared with any string that cannot be converted into a number (the operator is ==), and all returns true.

The following result will appear in the end:

1 var_dump('1000000'<'a');    //result: boolean true2 var_dump('a'<1);            //result: boolean true3 var_dump(1<'1000000');      //result: boolean true

It is still a little different from the conventional thinking. .

Reprint address: http://www.cnblogs.com/lwbqqyumidi/archive/2013/01/31/2887086.html


##

The above is the detailed content of Comparison of numbers and strings in PHP. For more information, please follow other related articles on the PHP Chinese website!

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