Home  >  Article  >  Backend Development  >  0 == 'ab' 好诡异的问题

0 == 'ab' 好诡异的问题

WBOY
WBOYOriginal
2016-06-06 20:51:461343browse

PHP代码:
var_dump(0 == 'ab');
输出是true,为什马?我今天晕了么???????

回复内容:

PHP代码:
var_dump(0 == 'ab');
输出是true,为什马?我今天晕了么???????

请参考这个问题:php里,为什么0 == 'abc'是成立的?

比较一个整数和字符串,则字符串会被转换为整数

PHP手册上的原话。

http://www.php.net/manual/zh/language...

以字母或下划线开头的字符串,类型转换为整型时,转换为0.
你试试var_dump(0 == '1ab');输出什么

参考
http://php.jz123.cn/language.types.ty...

著名的PHP 2B之处:用==进行判断,结果『可能』与你的预期相符。

在无法确定数据类型的前提下进行数据比对。建议使用===绝对等于来进行验证。不过要注意其他条件,比如,GET、POST、COOKIE等数据,都是string类型。

http://php.net/manual/zh/language.ope...
PHP.net有说明的.

这种情况必须使用===进行严格比较. 既比较类型,又比较值

PHP是一个弱类型的语言

请用===来判断0 == "ab"

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