带有OR运算符的PHP if语句

时间:2016-10-25 09:56:37

标签: php if-statement

我在我的网站上使用以下声明。我希望能够说明selected_plan_id是4,5,还是6.我使用的任何OR变体都会破坏我的代码。

 <?php if($CURRENT_USER['selected_plan_id'] == '5'): ?>

这是我到目前为止尝试的那些:

 <?php if($CURRENT_USER['selected_plan_id'] == '5' or '4' or '6'): ?>

:(错误

 <?php if($CURRENT_USER['selected_plan_id'] == '"5", "4", "6"'): ?>

:(为每个用户显示此内容

 <?php if($CURRENT_USER['selected_plan_id'] == '5'): or if($CURRENT_USER['selected_plan_id'] == '4'): or if($CURRENT_USER['selected_plan_id'] == '6'): ?>

:(错误

3 个答案:

答案 0 :(得分:2)

使用||声明。

<?php if($CURRENT_USER['selected_plan_id'] == '4' || $CURRENT_USER['selected_plan_id'] == '5' || $CURRENT_USER['selected_plan_id'] == '6'): ?>

使用in_array()

$arr = Array (4,5,6);
<?php if(in_array($CURRENT_USER['selected_plan_id'],$arr)): ?>

答案 1 :(得分:1)

<?php if( ( $CURRENT_USER['selected_plan_id'] == '4') || ( $CURRENT_USER['selected_plan_id'] == '5') || ( $CURRENT_USER['selected_plan_id'] == '6') ) : ?>

这将查看$CURRENT_USER['selected_plan_id']是4还是5还是6

答案 2 :(得分:0)

我的直觉会引导我in_array(),因为这似乎是表达这个想法的简单方法。
以下是&#34;或&#34;的手册页。运营商。 http://php.net/manual/en/language.operators.logical.php http://php.net/manual/en/language.operators.precedence.php