检查 PHP 中最接近的四的倍数还剩多少

Check how much left to the nearest multiple of four in PHP

我正在创建一个 magento 网站,我可以在其中购买至少 4 种产品,然后是 4 的倍数。我正在努力的事情是 else 语句应给出以下四的倍数的数字。例如如果此人有 5 件产品,则应显示 "Add 3 more products to your bundle" ,或者如果此人有 10 件产品,则显示“再向您的捆绑包添加 2 件产品”。我希望我描述得很好。

我的代码是:

      `<?php else: ?>
        <p class="empty"><?php echo $this->__('Add  more products to your bundle before checking out') ?></p>
          <?php endif ?>`

感谢您提供任何有用的答案。

您正在查找 modulus 函数。

$remainder = ($item_count % 4);

$a % $b | Modulus | Remainder of $a divided by $b.

您可能正在寻找 modulus-operator

$modulo = 11 % 4; //this will return 3 as the remainder
echo $modulo; //echos 3
if($modulo!=0){ //only execute this when items are needed
    $needed = 4 - $modulo;
    echo "you need ". $needed ." more items"; //echos "you need 1 more items
}