echo json_encode(0)."<br/>"; //输出"0" echo json_encode(null)."<br/>"; //输出"null" echo json_encode(false)."<br/>"; //输出"false" //被json_encode转码后,类型就是string //作判断就得用以下例子: if(json_encode(null) =="null"){ echo 1; }else{ echo 2; } //输出1
区分 null和 false意义很大,由于在数据库交互中,如select,查询结果是空则返回 null, 而出错则返回 false。php
区分了 null 和 false,才能够更加好地支持 事务数据库
Thinkphp的query方法数据库交互的错误判断:json
$model =new Model(); $order ="select * from asd where seatNm=1"; $res =$model->query($order); if(json_encode($res) !="false" && $res[0] ==null){ //结果集为空 echo 123; }else if(json_encode($res) =="false"){ //查询出错 echo 789; }else{ //返回非空结果集 echo 456; }
ps:我用的是tp3.1,不知道3.2是否是有更好的判断机制出现code
补充:还可以使用恒等于来作判断:===事务
$paramI =null; $paramII =false; $paramIII =0; if($paramI ===false){ exit("fail to judge null"); }else { exit(json_encode($paramI)); } //输出null if($paramII ===0){ exit("fail to judge false"); }else { exit(json_encode($paramII)); } if($paramI ===null){ exit("fail to judge 0"); }else { exit(json_encode($paramIII)); }