无法在php语句中找出运算符

igg*_*ion 1 php operators

我需要编辑一个函数,但我无法弄清楚是什么?和返回声明中的:false mean.我认为:是OR还是?我不知道.

public function hasPermission($name) { return $this->getVjGuardADUser() ? $this->getVjGuardADUser()->hasPermission($name) : false; }

有谁能为我解决这个问题?

Ben*_*mer 8

它是PHP的三元运算符.它就像是if/else表达式的简写.

您的扩展代码可能如下所示:

public function hasPermission($name)
{ 
    if ($this->getVjGuardADUser()) {
        return $this->getVjGuardADUser()->hasPermission($name)
    } else {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

一些来自php.net的示例代码:

// Example usage for: Ternary Operator
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];

// The above is identical to this if/else statement
if (empty($_POST['action'])) {
    $action = 'default';
} else {
    $action = $_POST['action'];
}
Run Code Online (Sandbox Code Playgroud)


fab*_*rik 5

它是三元运算符,如果构造则是一个简单的oneliner.