可能重复:
具有多个条件的php if语句
我有这个代码:
if(x==1 || x==2 || x==3 || x==4 )
无论如何只是简短吗?例如:
if(x==1||2||3||4 )
如果X = 1,2,3 OR 4,则该语句的含义是否为真?
先感谢您.
编辑:(感谢所有的回复,这里有一些澄清)
我有一个while循环但只想要调用特定的数据库条目.我的代码是最新的
<?php while(the_repeater_field('team_members','options') && get_sub_field('member_sort') == 1 : ?>
<div class="one_fourth">
<img src="<?php the_sub_field('image'); ?>" alt="" />
<p>
<?php the_sub_field('info'); ?>
<br />
<a href="mailto:<?php the_sub_field('email'); ?>"><?php the_sub_field('email'); ?></a>
</p>
</div>
<?php endwhile; ?>
Run Code Online (Sandbox Code Playgroud)
现在它与== 1完美配合,但我也想显示2,3,4.我只是不确定我应该如何执行代码来执行1 || 2 || 3 || 4
更新2:
好吧所以我使用了下面的代码,但我猜我的方法是错误的.下面的代码只显示了等于1的记录..但不是等于2,3,4的记录...我猜是因为while循环只运行一次,因为语句立即变为真.
<?php while(the_repeater_field('team_members','options') && in_array(get_sub_field('member_sort'),array(1,2,3,4))): ?>
<div class="one_fourth">
<img src="<?php the_sub_field('image'); ?>" alt="" />
<p>
<?php the_sub_field('info'); ?>
<br />
<a href="mailto:<?php the_sub_field('email'); ?>"><?php the_sub_field('email'); ?></a>
</p>
</div>
<?php endwhile; ?>
Run Code Online (Sandbox Code Playgroud)
if (in_array($x, array(1,2,3,4))
Run Code Online (Sandbox Code Playgroud)
甚至:
if (in_array($x, range(1, 4)))
Run Code Online (Sandbox Code Playgroud)
好的,这个问题已经发展了很多,但我认为现在的问题是你想要遍历所有的值但只在某些条件下做东西.您可以使用该continue语句中止当前迭代并立即转到下一个迭代.
<?php while (the_repeater_field('team_members','options')) : ?>
<?php if (!in_array(get_sub_field('member_sort'), array(1,2,3,4))) continue; ?>
... do stuff
<?php endwhile; ?>
Run Code Online (Sandbox Code Playgroud)