PHP: If statement matching several values prints nothing, elseif prints a div. Howto?
Good evening!
Being a total PHP-n00b I'm asking here hoping some clever brain out there can help me out. This is the case:
<?php if(wpsc_product_count() == 3 ) :?>
<div class="productseparator"></div>
<?php 开发者_运维知识库endif ; ?>
Now, what I want out of this is the following: If wpsc_product_count matches 3, 6, 9, 12, 15, 18, 21, 24, 27 or 30 - I would like it to print nothing at all. Every other value should print the .productseparator DIV.
Thanks a million in advance!
Use this function:
<?php if(wpsc_product_count() % 3 != 0) :?>
<div class="productseparator"></div>
<?php endif ; ?>
Try this
<?php
echo (wpsc_product_count() % 3 == 0) ? '' : '<div class="productseparator"></div>';
?>
if (!in_array(wpsc_product_count(), array(3,6,9,12,15,18,21,24,27,30)) {
echo '<div class="productseparator">';
}
relevant man page here.
One approach:
<?php
$cnt = wpsc_product_count();
if ($cnt > 0 && $cnt <= 30 && % 3 > 0) {
print '<div class="productseparator"></div>';
}
?>
using the '%' operator will give you the remainder of a/b.
精彩评论