【发布时间】:2012-05-29 21:19:53
【问题描述】:
我有这个代码:
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; ?>
现在它与 == 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; ?>
【问题讨论】:
-
较长的代码有什么问题?你想解决什么问题?
switchstatement 对你有用吗? -
@sarnold 我更新了我的问题。 @mario,谢谢你的链接。