Why check both isset() and !empty()

后端 未结 10 2506
难免孤独
难免孤独 2020-11-21 23:48

Is there a difference between isset and !empty. If I do this double boolean check, is it correct this way or redundant? and is there a shorter way

相关标签:
10条回答
  • isset($vars[1]) AND !empty($vars[1]) is equivalent to !empty($vars[1]).

    I prepared simple code to show it empirically.

    Last row is undefined variable.

    +-----------+---------+---------+----------+---------------------+
    | Var value | empty() | isset() | !empty() | isset() && !empty() |
    +-----------+---------+---------+----------+---------------------+
    | ''        | true    | true    | false    | false               |
    | ' '       | false   | true    | true     | true                |
    | false     | true    | true    | false    | false               |
    | true      | false   | true    | true     | true                |
    | array ()  | true    | true    | false    | false               |
    | NULL      | true    | false   | false    | false               |
    | '0'       | true    | true    | false    | false               |
    | 0         | true    | true    | false    | false               |
    | 0.0       | true    | true    | false    | false               |
    | undefined | true    | false   | false    | false               |
    +-----------+---------+---------+----------+---------------------+
    

    And code

    $var1 = "";
    $var2 = " ";
    $var3 = FALSE;
    $var4 = TRUE;
    $var5 = array();
    $var6 = null;
    $var7 = "0";
    $var8 = 0;
    $var9 = 0.0;
    
    function compare($var)
    {
        print(var_export($var, true) . "|" .
            var_export(empty($var), true) . "|" .
            var_export(isset($var), true) . "|" .
            var_export(!empty($var), true) . "|" .
            var_export(isset($var) && !empty($var), true) . "\n");
    }
    
    for ($i = 1; $i <= 9; $i++) {
        $var = 'var' . $i;
        compare($$var);
    }
    
    @print(var_export($var10, true) . "|" .
        var_export(empty($var10), true) . "|" .
        var_export(isset($var10), true) . "|" .
        var_export(!empty($var10), true) . "|" .
        var_export(isset($var10) && !empty($var10), true) . "\n");
    

    Undefined variable must be evaluated outside function, because function itself create temporary variable in the scope itself.

    0 讨论(0)
  • 2020-11-22 00:26

    if we use same page to add/edit via submit button like below

    <input type="hidden" value="<?echo $_GET['edit_id'];?>" name="edit_id">
    

    then we should not use

    isset($_POST['edit_id'])
    

    bcoz edit_id is set all the time whether it is add or edit page , instead we should use check below condition

    !empty($_POST['edit_id'])
    
    0 讨论(0)
  • 2020-11-22 00:29

    isset() tests if a variable is set and not null:

    http://us.php.net/manual/en/function.isset.php

    empty() can return true when the variable is set to certain values:

    http://us.php.net/manual/en/function.empty.php

    To demonstrate this, try the following code with $the_var unassigned, set to 0, and set to 1.

    <?php
    
    #$the_var = 0;
    
    if (isset($the_var)) {
      echo "set";
    } else {
      echo "not set";
    }
    
    echo "\n";
    
    if (empty($the_var)) {
      echo "empty";
    } else {
      echo "not empty";
    }
    ?>
    
    0 讨论(0)
  • 2020-11-22 00:31

    "Empty": only works on variables. Empty can mean different things for different variable types (check manual: http://php.net/manual/en/function.empty.php).

    "isset": checks if the variable exists and checks for a true NULL or false value. Can be unset by calling "unset". Once again, check the manual.

    Use of either one depends of the variable type you are using.

    I would say, it's safer to check for both, because you are checking first of all if the variable exists, and if it isn't really NULL or empty.

    0 讨论(0)
提交回复
热议问题