在PHP中,我对null,0,false等变量相关的细节进行了初步探索,代码和输出结果粘贴如下,值得思考。
<?php
echo '针对 0,null,false,"" 的研究';
echo "<br/>";
echo "<br/>";
echo "<br/>0 == false 为 " . (string)(0 == false);
echo "<br/>'0' == false 为 " . (string)('0' == false);
echo "<br/>null == false 为" . (string)(null == false);
echo "<br/>null == 0 为" . (string)(null == 0);
echo "<br/>";
echo "<br/>";
echo "0 === false 为 " . (string)(0 === false);
echo "<br/>'0' === false 为 " . (string)('0' === false);
echo "<br/>null === false 为" . (string)(null === false);
echo "<br/>null === 0 为" . (string)(null === 0);
echo "<br/>";
echo "<br/>";
echo "<br/>'12' == 12 为 " . (string)('12' == 12);
echo "'12' === 12 为 " . (string)('12' === 12);
echo "关于empty函数:<br/>";
$var = 0;
echo "empty(0) is " . empty($var);
$var = "0";
echo "<br/>empty('0') is " . empty($var);
$var = false;
echo "<br/>empty(false) is " . empty($var);
$var = "false";
echo "<br/>empty('false') is " . empty($var);
$var = null;
echo "<br/>empty(null) is " . empty($var);
$var = "null";
echo "<br/>empty('null') is " . empty($var);
?>
输出结果如下:
针对 0,null,false,"" 的研究
0 == false 为 1
'0' == false 为 1
null == false 为1
null == 0 为1
0 === false 为
'0' === false 为
null === false 为
null === 0 为
'12' == 12 为 1'12' === 12 为 关于empty函数:
empty(0) is 1
empty('0') is 1
empty(false) is 1
empty('false') is
empty(null) is 1
empty('null') is