Why check both isset() and !empty()

后端 未结 10 2507
难免孤独
难免孤独 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条回答
  •  抹茶落季
    2020-11-22 00:12

    This is completely redundant. empty is more or less shorthand for !isset($foo) || !$foo, and !empty is analogous to isset($foo) && $foo. I.e. empty does the reverse thing of isset plus an additional check for the truthiness of a value.

    Or in other words, empty is the same as !$foo, but doesn't throw warnings if the variable doesn't exist. That's the main point of this function: do a boolean comparison without worrying about the variable being set.

    The manual puts it like this:

    empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.

    You can simply use !empty($vars[1]) here.

提交回复
热议问题