PHP中区分null和false的方法

WBOY
Release: 2016-06-20 12:34:24
Original
1146 people have browsed it

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
Copy after login

区分 null和 false意义很大,因为在数据库交互中,如select,查询结果是空则返回 null, 而出错则返回 false。

区分了 null 和 false,才可以更加好地支持 事务


Thinkphp的query方法数据库交互的错误判断:

$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;}
Copy after login

ps:我用的是tp3.1,不知道3.2是不是有更好的判断机制出现



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template