知识介绍
-
php中有两种比较的符号
==
与===
-
===
在进行比较的时候,会先判断两种字符串的类型是否相等,再比较 -
==
在进行比较的时候,会先将字符串类型转化成相同,再比较
-
-
如果比较一个数字和字符串或者比较涉及到数字内容的字符串,则字符串会被转换成数值然后按照转换后的数值来进行比较
-
例子:
<?php var_dump("admin"==0); //true var_dump("1admin"==1); //true var_dump("admin1"==1) //false var_dump("admin1"==0) //true var_dump("0e123456"=="0e4456789"); //true ?>
-
核心:
- 该字符串的开始部分决定了它的值,如果该字符串以合法的数值开始,则使用该数值,否则其值为0。
- 这就可以解释:
var_dump("admin1"==1) //false
而var_dump("1admin"==1); //true
- 这就可以解释:
- 如果该字符串没有包含
.
,e
,E
并且其数值值在整形的范围之内该字符串被当作int来取值,其他所有情况下都被作为float来取值- 这就可以解释
"0e123456"=="0e456789"
相互比较的时候,会将0e这类字符串识别为科学技术法的数字,0的无论多少次方都是零,所以相等
- 这就可以解释
- 该字符串的开始部分决定了它的值,如果该字符串以合法的数值开始,则使用该数值,否则其值为0。