Home  >  Article  >  Backend Development  >  if(零=='abc')为什么会通过

if(零=='abc')为什么会通过

WBOY
WBOYOriginal
2016-06-13 13:22:40912browse

if(0=='abc')为什么会通过
if(0=='abc')为什么会表示为真呢?求指教原理

------解决方案--------------------
if (0 == intval('abc')) 弱类型,后面的会转为前面的类型,再比较

你需要用 0 === 'abc'
------解决方案--------------------
因为'abc'被强转成了(int)'abc',和0 == intval('abc')一样。。

这个行为没注意过啊。。 我擦,PHP这是要逆天。
------解决方案--------------------
字符串和数字想判断,那么会将字符串取int值,所以abc在int的意义上就是0,所以相等,你可以用===来判断,这样连带着类型也判断了
------解决方案--------------------
这种比较是不严密的。至少要用 ===
------解决方案--------------------

PHP code
var_dump((int) 'abc'); //int(0)
<br><font color="#e78608">------解决方案--------------------</font><br>ls两位已解答。if(0=='abc')  等于  if(0==intval('abc')) <div class="clear">
                 
              
              
        
            </div>
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