Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php If语句未输出预期结果_Php_Cakephp_If Statement - Fatal编程技术网

Php If语句未输出预期结果

Php If语句未输出预期结果,php,cakephp,if-statement,Php,Cakephp,If Statement,我在cakePHP应用程序中有一个if语句,我不明白为什么它没有达到我预期的效果 public function isAuthorized($user) { if ($user['role'] === 'admin'){ return true; } if ((in_array($this->action, array('view', 'index')))&&($user['role'

我在cakePHP应用程序中有一个if语句,我不明白为什么它没有达到我预期的效果

public function isAuthorized($user) {         
        if ($user['role'] === 'admin'){
            return true;
        }
        if ((in_array($this->action, array('view', 'index')))&&($user['role'] === 'senior' || 'junior')) {

          return true;
        }
        return false;
       }
我希望如果有一个角色为“代理”的用户,它会拒绝所有操作

如果我改为使用Everything is rosey,只是不确定为什么在将布尔值设置为True之前不检查这两个参数

public function isAuthorized($user) {
        if ($user['role'] === 'admin'){
            return true;
        }
        if ($user['role'] == 'agent'){
            return false;
        }
        if (in_array($this->action, array('edit', 'add', 'delete'))) {
            if ($user['role'] == 'senior' || 'junior') {
                return false;
            }

        }
        return true;
    }
有什么想法吗?
谢谢

您的测试中有一项是错误的,并且评估结果总是正确的

if($user['role'] === 'senior' || 'junior'){
  //will always be true 
}
因为您将
'junior'
作为布尔值进行计算,这在PHP中是正确的

你的情况应该是:

if($user['role'] == 'senior' || $user['role'] == 'junior'){
  ...
}
请注意,您也可以这样写:

if(in_array($user['role'], array('senior', 'junior'))){

}