An explanation of the Boolean in PHP

Source: Internet
Author: User
Tags type null
In programming, many times we need to make a Boolean decision about the variable. But sometimes the result of the procedure is different from what we expected, today we will test the situation of PHP in various situations.

How to tell if a variable is empty, say no more, direct code

       $testIsset = "Assigned value";        $results = Array (); Array_push ($results, ["state" = "=" variable is assigned "," testfun "=" isset () "," Result "=>isset ($testIsset)," resultdes "=        > "Isset returns True"]); Array_push ($results, ["state" = = "variable is assigned", "testfun" = "Empty ()", "Result" =>empty ($testIsset), "resultdes" =        > "Empty returns False"]); Array_push ($results, ["state" = "=" variable is assigned "," testfun "=" is_null () "," Result "=>is_null ($testIsset)," Resultdes        "= =" Is_null returns false "]); Array_push ($results, ["state" = "=" variable is assigned "," testfun "=" var = = null "," result "= = $testIsset ==null," resultdes "=        > "var = = null returns false"]); Array_push ($results, ["state" = "=" variable is assigned "," testfun "=" var = = = NULL "," result "= = $testIsset ===null," Resultdes "        = = "var = = null returns false"]);        Unset ($testIsset); Array_push ($results, ["state" = + "variable unset", "testfun" = "isset ()", "Result" =>isset ($testIsset), "Resultdes"        = "Isset returns false"]); Array_push ($results, ["state" = + "variable unset", "testfun" and "=" emptY () "," Result "=>empty ($testIsset)," resultdes "=" Empty Returns True "]); Array_push ($results, ["state" = = "Variable unset", "testfun" = "is_null ()", "result" = "", "resultdes" and "=" function call times wrong        "]); Array_push ($results, ["state" = = "Variable unset", "testfun" = "var = = null", "result" = "", "resultdes" + "function call times wrong        "]); Array_push ($results, ["state" = + "variable unset", "testfun" = "var = = = NULL", "result" = "", "resultdes" = "        function call times wrong "]);        $testIsset = null; Array_push ($results, ["state" = "=" variable is set to null "," testfun "=" isset () "," Result "=>isset ($testIsset),"        Resultdes "=" Isset returns false "]); Array_push ($results, ["state" = = "variable is set to null", "testfun" = "Empty ()", "Result" =>empty ($testIsset), "        Resultdes "=" and "Empty returns True"]); Array_push ($results, ["state" = "=" variable is set to null "," testfun "=" is_null () "," Result "=>is_null ($testIsset),"        Resultdes "=" Is_null Returns True "]); Array_push ($results, ["state" = "=" variable is set to null "," testfun "=" var = = null "," result "= = $testIsset==null, "resultdes" = "var = = NULL Returns TRUE"]); Array_push ($results, the ["state" and "=" variable is set to null "," testfun "=" var = = = NULL "," result "and" = "= $testIsset ===null,"        Resultdes "= =" var = = = NULL Returns TRUE "]);        $testIsset = 0; Array_push ($results, ["state" = "=" variable is set to number 0 "," testfun "=" isset () "," Result "=>isset ($testIsset)," Resultdes        "= =" Isset returns True "]); Array_push ($results, the ["state" and "=" variable is set to the number 0 "," testfun "=" Empty () "," Result "=>empty ($testIsset)," Resultdes        "= =" Empty Returns True "]); Array_push ($results, the ["state" and "=" variable is set to the number 0 "," testfun "=" is_null () "," Result "=>is_null ($testIsset),"        Resultdes "=" Is_null returns false "]); Array_push ($results, the ["state" and "=" variables are set to the number 0 "," testfun "=" var = = null "," result "and" = "=" ==null ","        Resultdes "= =" var = = NULL Returns TRUE "]); Array_push ($results, the ["state" and "=" variable is set to the number 0 "," testfun "=" var = = = NULL "," result "= = $testIsset ===null,"        Resultdes "= =" var = = = NULL returns false "]);        $testIsset = "0"; Array_push ($results,["state" and "=" variables are set to string 0 "," testfun "=" isset () "," Result "=>isset ($testIsset)," resultdes "and" = "        Isset returns True "]); Array_push ($results, ["state" = "=" variable is set to String 0 "," testfun "=" Empty () "," Result "=>empty ($testIsset),"        Resultdes "=" and "Empty returns True"]); Array_push ($results, ["state" = "=" variable is set to String 0 "," testfun "=" is_null () "," Result "=>is_null ($testIsset),"        Resultdes "=" Is_null returns false "]); Array_push ($results, the ["state" and "=" variables are set to string 0 "," testfun "=" var = = null "," result "and" = "," ==null ","        Resultdes "= =" var = = null returns false "]); Array_push ($results, the ["state" and "=" variable is set to String 0 "," testfun "=" var = = = NULL "," result "= = $testIsset ===null,"        Resultdes "= =" var = = = NULL returns false "]);        $testIsset = false; Array_push ($results, ["state" = "=" variable is set to False "," testfun "=" isset () "," Result "=>isset ($testIsset),"        Resultdes "=" Isset Returns True "]); Array_push ($results, ["state" = = "variable is set to False", "testfun" = "Empty ()", "Result" =>empty ($testIsset), " Resultdes "= "Empty Returns True"]); Array_push ($results, ["state" = "=" variable is set to False "," testfun "=" is_null () "," Result "=>is_null ($testIsset),"        Resultdes "=" Is_null returns false "]); Array_push ($results, the ["state" and "=" variables are set to False, "testfun" = "var = = null", "result" and "=" = "==null", "        Resultdes "= =" var = = NULL Returns TRUE "]); Array_push ($results, ["state" = "=" variable is set to False "," testfun "=" var = = = NULL "," result "= = $testIsset ===null,"        Resultdes "= =" var = = = NULL returns false "]);        $testIsset = ""; Array_push ($results, ["state" = "=" variable is set to an empty string "," testfun "=" isset () "," Result "=>isset ($testIsset),"        Resultdes "=" Isset Returns True "]); Array_push ($results, ["state" = "=" variable is set to an empty string "," testfun "=" Empty () "," Result "=>empty ($testIsset),"        Resultdes "=" and "Empty returns True"]); Array_push ($results, ["state" = "=" variable is set to an empty string "," testfun "=" is_null () "," Result "=>is_null ($testIsset),"        Resultdes "=" Is_null returns false "]); Array_push ($results, ["state" = "=" variable is set to an empty string "," testfun "=" V "AR = = null "," result "= = $testIsset ==null," resultdes "=" var = = NULL Returns TRUE "]); Array_push ($results, the ["state" and "=" variable is set to an empty string "," testfun "=" var = = = NULL "," result "= = $testIsset ===null," Resultdes "= =" var = = = NULL returns false "]);

The results are as follows:

Conclusion:
Isset returns false for uninitialized or null variables, indicating that it has not been initialized
Empty for uninitialized, NULL, number 0, string 0,false, null string, will return true, indicating that the variable is empty, so empty is to strongly convert the variable to a Boolean type, and then return
Is_null returns True when the variable is null
var = = NULL when the variable is null, the number 0,false, and the empty string returns True
var = = NULL Returns TRUE if the variable is null

The Boolean type in PHP is detailed

When converted to Boolean, the following values are considered FALSE;
Boolean (FALSE);
Int (0)
Float (0.0)
Empty string and string "0"
An array that does not include any elements
Special type NULL (including variables that have not been set)

When converted to Boolean, the following values are considered FALSE;

Boolean to string when,true=>1,false=> "";

Related recommendations:

Course PHP Fun Class

PHP Quick Start Summary

Course PHP Bottom Analysis Video tutorial

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.