Home  >  Article  >  Backend Development  >  见鬼啦

见鬼啦

WBOY
WBOYOriginal
2016-06-23 13:56:19831browse

<?php     $huancuns='[{"id":"1","time":"0"},{"id":"2","time":"0"}]';            if($huancuns=="null"||$huancuns==''||$huancuns==0)            {                             echo ("311");            }            else            {                               echo("211");            }?>


为什么输出的是311.。。坑爹见鬼了?


回复讨论(解决方案)

$huancuns==0 不是成立吗?
$huancuns === 0 才不成立

$huancuns==0 不是成立吗?
$huancuns === 0 才不成立



'[{"id":"1","time":"0"},{"id":"2","time":"0"}]' 这个等于0???

字符串转换成数字,如果字符串开头不是数字的话,结果就是0。

这有什么可奇怪的?

var_dump('[{"id":"1","time":"0"},{"id":"2","time":"0"}]' == 0);
bool(true)

这有什么可奇怪的?

var_dump('[{"id":"1","time":"0"},{"id":"2","time":"0"}]' == 0);
bool(true)



学习了,呵,还不如去掉  == 0 

这种判断本来就该写作
if(empty($huancuns)) {
  //为空时的处理
}

判断变量是否是null,0 “” “0”……用empty($str)

php 比较,当两端有一端是数字,都会转成数字比较的。
可以这样写

            if(!$huancuns) {                echo ("311");            }else{                echo("211");            }

empty或者!都可以 
==和 不加()都会产生奇怪的现象...
这是和机制相关的

$huancuns==0 // 去掉这个条件可能是把这个字符串转化成了数字,但是转换失败了,直接就给转成了 0 。

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