Detecting insufficient PHP variables: FALSE vs NULL vs unset() vs empty()?

前端 未结 3 880
花落未央
花落未央 2021-01-07 10:35

What is the best way to define that a value does not exist in PHP, or is not sufficent for the applications needs.

$var = NULL, $var = array()

3条回答
  •  灰色年华
    2021-01-07 11:13

    My vote goes for unset(), because non existing variables should generate an notice when used.

    Testing gets a bit more complicated, because isset() wil be false if the variable is "not existings" or null. (Language designflaw if you'd ask me)

    You could use array_key_exists() for valiables in $GLOBALS, $_POST etc.
    For class properties there is property_exists().

提交回复
热议问题