如何使这行PHP更短?

我也有这个循环,它将计算输出

while ($wp_query->have_posts()) : $wp_query->the_post(); $current++; $current2++;

然后要调用适当的html类,我需要在我的设计中使用它:

<div class="span4 <?PHP if($current == 0) { echo 'first'; } elseif($current == 1) { echo 'second'; } elseif($current == 2) { echo 'first'; } elseif($current == 3) { echo 'second'; } ?>" id="<? echo $current; ?>">

$count从0开始,$count2从1开始.输出应如下所示:如果0 = first,1 = second,2 = first,3 = second等.如何使用无限数量的$count使该表达式更短?希望我的问题清楚.感谢您为我提供帮助的人.

解决方法:

如果我了解您在这里的要求,我想您想将div替换为“第一”和“第二”类吗?

因此,您可以使用%模运算符(请参见http://php.net/manual/en/internals2.opcodes.mod.php)

<div class="span4 <?PHP echo ($current % 2 === 0 ? 'first' : 'second'); ?>" id="<? echo $current; ?>">

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...