checking if a number is divisible by 6 PHP

前端 未结 11 1707
不思量自难忘°
不思量自难忘° 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:45

    $num += (6-$num%6)%6;
    

    no need for a while loop! Modulo (%) returns the remainder of a division. IE 20%6 = 2. 6-2 = 4. 20+4 = 24. 24 is divisible by 6.

提交回复
热议问题