PHP < 8
0和任何字符串比较都会先把字符串转换为整型再进行比较
- 如:'a' => 0,'1a' => 1,'1a234' => 1
- 由于第一个操作数是数字(0),而第二个操作数是字符串('a'),因此字符串也会转换为数字,再进行数字对比。
字符串数据类型的手册页定义了字符串到数字转换的完成方式:
When a string is evaluated in a numeric context, the resulting value and type are determined as follows.
If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.
在这种情况下,字符串是'e',因此它将被评估为float:
The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.
由于'a'不是以有效的数字数据开头,因此它的计算结果为float 0。
评论/回复