Methods for distinguishing between null and false in PHP

Source: Internet
Author: User
echo json_encode (0). "
"; Output "0" echo json_encode (null). "
"; Output "null" echo Json_encode (false). "
"; The output "false"//is Json_encode transcoded, the type is string//to make a judgment to use the following example: if (Json_encode (null) = = "Null") { echo 1;} Else{ echo 2;} Output 1

Distinguishing between null and false is significant because in database interactions, such as SELECT, NULL is returned when the query result is NULL, and error returns false.

distinguishes between null and false in order to better support transactions


thinkphp Query Method Database interaction error Judgment:

$model =new model (); er = "select * from ASD where seatnm=1"; $res = $model->query (er); if (Json_encode ($res)! = " False "&& $res [0] ==null) {    //result set is empty    echo 123;} else if (Json_encode ($res) = = "false") {    //Query error    echo 789;} else{     //return non-empty result set    echo 456;}

PS: I use tp3.1, do not know 3.2 is not a better judgment mechanism appears

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.