【问题标题】:PHP: If statement matching several values prints nothing, elseif prints a div. Howto?PHP:如果匹配多个值的语句不打印任何内容,elseif 打印一个 div。如何?
【发布时间】:2011-08-19 20:06:56
【问题描述】:
晚上好!
作为一个完整的 PHP-n00b 我在这里问希望一些聪明的大脑可以帮助我。
是这样的:
<?php if(wpsc_product_count() == 3 ) :?>
<div class="productseparator"></div>
<?php endif ; ?>
现在,我想要的是以下内容:
如果 wpsc_product_count 匹配 3、6、9、12、15、18、21、24、27 或 30 - 我希望它什么也不打印。每个其他值都应该打印 .productseparator DIV。
提前一百万谢谢!
【问题讨论】:
标签:
php
wordpress
if-statement
multiple-value
【解决方案1】:
使用此功能:
<?php if(wpsc_product_count() % 3 != 0) :?>
<div class="productseparator"></div>
<?php endif ; ?>
【解决方案2】:
试试这个
<?php
echo (wpsc_product_count() % 3 == 0) ? '' : '<div class="productseparator"></div>';
?>
【解决方案3】:
if (!in_array(wpsc_product_count(), array(3,6,9,12,15,18,21,24,27,30)) {
echo '<div class="productseparator">';
}
相关man page here.
【解决方案4】:
一种方法:
<?php
$cnt = wpsc_product_count();
if ($cnt > 0 && $cnt <= 30 && % 3 > 0) {
print '<div class="productseparator"></div>';
}
?>
使用 '%' 运算符将为您提供 a/b 的剩余部分。