PHP IF 语句帮助 - OR AND 等

发布于 2024-12-01 19:36:59 字数 486 浏览 1 评论 0原文

我有一个小问题。我希望创建一个 IF 语句,其反应如下:

  • 如果用户不是管理员 (forum_admin) 并且他们不是票证的所有者 (userid),但版主可以查看票证 (allow_moderator_tickets) 并且用户是版主 ( forum_moderator),那么他应该被允许查看票证。

我如何使用 PHP if 语句获得它。

到目前为止我有这个:

 //If user is not allowed to view.  
    if($userdata['forum_admin']==0 && $ticketDetails['userid']!=$userdata['id'] || $sdata['allow_moderator_ticket']==0 && $userdata['forum_moderator']==1)
        redirect("?i=a");

I have a little problem. I wish to create an IF statement, that shall react like this:

  • If user is NOT admin (forum_admin) AND they are not the owner of the ticket (userid), BUT moderators are allowed to view tickets (allow_moderator_tickets) AND user is moderator (forum_moderator), then he should be allowed to view the ticket.

How can I obtain this with a PHP if statement.

So far I have this:

 //If user is not allowed to view.  
    if($userdata['forum_admin']==0 && $ticketDetails['userid']!=$userdata['id'] || $sdata['allow_moderator_ticket']==0 && $userdata['forum_moderator']==1)
        redirect("?i=a");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缺⑴份安定 2024-12-08 19:36:59
 if ( isAdmin || (allowModerators && isModerator) || userID == ticketUser ) allowToSeeTicket

 if ( ! (isAdmin || (allowModerators && isModerator) || userID == ticketUser) ) notAllowToSeeTicket
 if ( isAdmin || (allowModerators && isModerator) || userID == ticketUser ) allowToSeeTicket

 if ( ! (isAdmin || (allowModerators && isModerator) || userID == ticketUser) ) notAllowToSeeTicket
铜锣湾横着走 2024-12-08 19:36:59

不能混合使用 and 和 or 运算符。彼此都会宠坏另一个。
您应该使用括号对表达式进行分组,就像算术一样,

重构您的代码以使其可读会很好

$admin = ($userdata['forum_admin']);
$owner = ($ticketDetails['userid'] == $userdata['id']);
$moder = ($sdata['allow_moderator_ticket'] == 1 AND $userdata['forum_moderator']==1)

if !($admin OR $moder OR $owner) {
    redirect("?i=a");
}

you cannot mix and and or operators. each other will spoil another.
you should group your expressions, using parenthesis, much like in arithmetics

it would be nice to refactor your code to make it readable

$admin = ($userdata['forum_admin']);
$owner = ($ticketDetails['userid'] == $userdata['id']);
$moder = ($sdata['allow_moderator_ticket'] == 1 AND $userdata['forum_moderator']==1)

if !($admin OR $moder OR $owner) {
    redirect("?i=a");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文