How to get the multiple of some value in PHP?

10

I need to do a search on the database and according to the value found within a range of values should return a specific value.

For example, if you find values from 1 to 4, you should return 1, finding from 5 to 8, you should return 2.

The multiple of a given value is required.

    
asked by anonymous 01.01.2014 / 15:28

1 answer

11

What you need is the ceil () function, which rounds values up.

So you can use:

$valorRetornado = ceil($seuValor / 4);

For example:

echo ceil(1 / 4);     // dá 1
echo ceil(4 / 4);     // dá 1
echo ceil(5 / 4);     // dá 2
echo ceil(8 / 4);     // dá 2
echo ceil(54654 / 4); // dá 13664
    
01.01.2014 / 15:39