PHP intdiv,为什么分割前会有+2?

时间:2017-06-21 03:15:41

标签: php math

以下代码有intdiv,但是在除数之后有一个+2,在除数之前,为什么?

       $sitem_qty=0;
       foreach($order_items as $item_id => $item) {
            if ($item['product_id']==$S_ID) {
                $sitem_qty += intval($item['qty']);
            }
        }
        //number of boxes in this order
        $shipping_boxes = intdiv($sitem_qty+2,3); // what does the +2 do and what does it equal? Why do this?
        //remaining capacity
        $capacity_remaining = $shipping_boxes*5;

+2做什么??? intdiv($ sitem_qty + 2,3)

1 个答案:

答案 0 :(得分:1)

我刚刚根据您提供的信息和变量名称进行了快速测试,我相信他们会使用+2来使其迭代3(每箱可能有3个项目?) 我可能错了,但这是我用来做出决定的代码。

<?php  
$s1 = 1; $s2 = 2; $s3 = 3; $s4 = 4; $s5 = 5;  
$s6 = 6; $s7 = 7; $s8 = 8; $s9 = 9; $s10 = 10;

echo intdiv($s1+2,3)."<br>"; echo intdiv($s2+2,3)."<br>"; 
echo intdiv($s3+2,3)."<br>"; echo intdiv($s4+2,3)."<br>";  
echo intdiv($s5+2,3)."<br>"; echo intdiv($s6+2,3)."<br>"; 
echo intdiv($s7+2,3)."<br>"; echo intdiv($s8+2,3)."<br>"; 
echo intdiv($s9+2,3)."<br>"; echo intdiv($s10+2,3)."<br>"; 
?>

这是我得到的用于比较的输出。 1 1 1 2 2 2 3 3 3 4

相关问题