Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to compare PHP Variables and Arrays with null or empty values

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 695
    Comment on it

    Sometimes it is a bit confusing for developer to select right compare statement to check variables or arrays as there are lots of compare functions like "isset()", "!" operator, "is_null()", "empty()" etc.

    So we can use a straight forward approach to do this:

    1: For boolean value, use:

    if ($var) {}
    

    or

    if (!$var) {}
    

    2: Null or false value:

     $var = '';
     if (empty($var))
    

    3: If variable is set or not:

    if (isset($var)) {}
    

    4: If we are just checking for NULL value

    if (is_null($var)) {} or if ($var == NULL)
    

    For more information to select right compare statement, you can read PHP type comparison

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: