如何用感叹号写isset

时间:2012-08-28 16:29:21

标签: php

我只需要澄清。

我的代码如下:

if ((!$username) && (!$userid))

但我希望在此代码中包含一个isset。输入的正确方法如下:

if ((!isset($username)) && (!isset($userid))) 

由于

4 个答案:

答案 0 :(得分:5)

如果你想要真正喜欢,isset()可以接受多个参数,如果设置了所有参数则返回true。所以,你可以做到

if (!isset($username,$userid))

编辑:这不是您要求的。当 $ username或$ userid未设置时,将执行if块中的代码。在原始帖子中,您希望在 $ username和$ userid未设置时执行if。

答案 1 :(得分:2)

实际上,如果你有!$x这个词,并且你想添加!isset($x),那么使用empty可能会更好:

if (empty($username) && empty($userid)) {
    // both $username and $userid are either non-existent or "empty"
    // "empty" is defined as: evaluates to false (see manual page)
}

它还简化了条件,没有使用否定:)

另请参阅:empty()

答案 2 :(得分:1)

您的第二段代码将有效。但是这样也会这样:

if( !isset($username) && !isset($userid) )

答案 3 :(得分:0)

是的,这很好,应该有用。