if 语句的单独子句?
有没有办法在 if()
语句中包含多个子句?
例如:
if( ($username=='textUser' && $role=='admin') || ($admin=='yes'))
{
// If the username AND role are set to admin OR if the admin is set to 'yes'
}
else
{
// Neither clauses of the if statement are true
}
也许这实际上是正确的代码,我没有尝试过 - 但如果没有,有人能告诉我怎么做吗? :)
Is there any way to have multiple clauses in an if()
statement?
For instance:
if( ($username=='textUser' && $role=='admin') || ($admin=='yes'))
{
// If the username AND role are set to admin OR if the admin is set to 'yes'
}
else
{
// Neither clauses of the if statement are true
}
Perhaps this is actually the correct code, i have no tried it - but if not, could anyone tell me how? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这实际上是正确的代码。除了
||
,您还可以使用或
(尽管它们在 运算符优先级,与当前的情况无关,但值得注意,欢呼@Timothy。)我倾向于将每个条件放入
$a == b
放入括号中:($a == b)
以避免复杂的if
语句中出现有趣的情况。That is actually the correct code. Instead of
||
, you can also useor
(although they differ slightly in operator precedence, not relevant in the case at hand but worth noting, cheers @Timothy.)I tend to put every condition
$a == b
into brackets:($a == b)
to avoid funny situations in complexif
statements.