checking if a number is divisible by 6 PHP

前端 未结 11 1704
不思量自难忘°
不思量自难忘° 2020-12-13 02:15

I want to check if a number is divisible by 6 and if not I need to increase it until it becomes divisible.

how can I do that ?

11条回答
  •  借酒劲吻你
    2020-12-13 02:50

    Use the Mod % (modulus) operator

    if ($x % 6 == 0) return 1;
    
    
    function nearest_multiple_of_6($x) {
        if ($x % 6 == 0) return $x;    
    
        return (($x / 6) + 1) * 6;
    }
    

提交回复
热议问题