Php Can';I don’我不能使一个基本的或陈述正确地起作用
在我的PHP函数中,我想Php Can';I don’我不能使一个基本的或陈述正确地起作用,php,Php,在我的PHP函数中,我想返回如果用户不是版主或如果用户不是post author等。请参阅以下基本语句: $mod = false; $status = 'pending'; $currentuser = 22; $author = 22; if ( (!$mod) || ( ($status != 'pending') && ($currentuser != $author) ) ) { return; } 因此,在本例中,函数不应返回,因为$curre
返回如果用户不是版主或如果用户不是post author等。请参阅以下基本语句:
$mod = false;
$status = 'pending';
$currentuser = 22;
$author = 22;
if ( (!$mod) || ( ($status != 'pending') && ($currentuser != $author) ) ) {
return;
}
因此,在本例中,函数不应返回,因为$currentuser
与$author
和$status
匹配
我做错了什么?(!$mod)
是真的。如果
条件被评估为true,则为
你最终会有:
if ( true || anotherCondition ) {
return;
}
在这种情况下,其他条件是什么并不重要。其计算结果为true
您的代码与您需要的非常接近
if ( (!$mod) && ($status != 'pending') && ($currentuser != $author) ) {
// if the user is not a moderator AND
// the status is not pending AND
// the user is not the owner, then
return;
}
因为用户不是mod,它会使if语句的后半部分短路,将其设为and而不是or,只有当这两个语句都为true时,它才会返回
另一个选择是:
if(($mod) || ( ( ($status == 'pending') && ($current user == $author) ) ) {
// do logic here
}
else {
// user is not a mod and user is not the pending author
return;
}
是的,这是我已经理解的。我的问题是,在我的情况下,我如何做一个恰当的OR陈述?所以我想检查$mod是否已设置,post author是否为currentuseretc@HenrikPettersonif(($mod)
而不是if(!$mod)
@George你在说什么,如果它是一个mod,它应该返回?这根本不是用户要求的。如果用户不是版主,或者用户不是博文作者,我想返回。@insidensin我误解了OP想要什么。@Henrikpeterson,这是你需要的吗?(更新答案)你应该更好地解释“等”是什么意思我的理解是,你所需要的是|
,而不是&&
到处都是,括号少了很多。但那太容易了,不是吗。我想你想用另一个&&;而不是为什么否决票?答案符合ops的问题,并解释了原始代码的错误。嗯,因为它不符合OP的问题?你的中心&
应该是一个|
,正如OP的帖子所说,他想要的用户不是mod或者不是作者。当用户不是mod或者不是作者时,他想要返回,如果是OR,那么如果$mod为false,它将总是返回,这不是OP想要的什么?你真的自相矛盾。你真的自相矛盾吗可能需要查看|
的工作原理。如果你是版主,如果你不是发帖人,你就会失败。如果你是发帖人,如果你不是版主,你就会失败。这是他的问题的解决方案,他的问题是否合乎逻辑就是问题。在OP的原始帖子中,如果用户不是版主,它将始终返回,在我的原始答案如果op不是主持人,前半部分将为真,对于整个if,第二部分也需要为真;这意味着只有当用户不是主持人且用户不是当前用户时,程序才会返回。在op的示例中,用户不是mod,因此第一部分为真,但因为用户是当前用户,stats处于挂起状态,if语句总体上为false,程序不会返回,就像OP ask一样。
if(($mod) || ( ( ($status == 'pending') && ($current user == $author) ) ) {
// do logic here
}
else {
// user is not a mod and user is not the pending author
return;
}