Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP how to use range() for float numbers

Tags:

php

Is there a way to use PHP range() for a really small number? like below?

range(0.54,0.58);

Doing this creates an error.

range(): step exceeds the specified range in

like image 904
Sarotobi Avatar asked Jul 11 '18 12:07

Sarotobi


2 Answers

The third parameter is the step.
Reading the documentation is a very good place to start.

If you read you will see that you can set the step to something that can increment between your limits.
The error message is simply telling you that it cannot increment 0.54 by 1 without going over 0.58 - think about, it 1 + 0.54 is more than 0.58!

Using this line instead will give you the desired output.

range(0.54, 0.58, 0.01);
like image 80
JustCarty Avatar answered Oct 20 '22 01:10

JustCarty


The error message is self-explanatory: the default value of $range (the third argument of range()) is 1 and using it produces an empty range.

Provide a third argument to range(). From the value of $start and $end I think 0.01 is the step you need:

range(0.54, 0.58, 0.01)

Check it out: https://3v4l.org/IcUAh

like image 25
axiac Avatar answered Oct 20 '22 00:10

axiac