Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Can range() be used for fractions?

Could it be possible to use the range() function in PHP to generate a list of fractions or decimals?

like image 338
Apophenia Overload Avatar asked Jan 20 '23 17:01

Apophenia Overload


1 Answers

Yeah, if you specify the step (third parameter). This parameter is only available in PHP 5, but you should be using that by now anyway.

For example, to generate decimals between 0 and 1, inclusive, in intervals of 0.1:

print_r(range(0, 1, 0.1));

Output:

Array
(
    [0] => 0
    [1] => 0.1
    [2] => 0.2
    [3] => 0.3
    [4] => 0.4
    [5] => 0.5
    [6] => 0.6
    [7] => 0.7
    [8] => 0.8
    [9] => 0.9
    [10] => 1
)
like image 102
BoltClock Avatar answered Jan 28 '23 14:01

BoltClock