Php If语句未输出预期结果
我在cakePHP应用程序中有一个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'
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'))){
}