8

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

3
  • what do you want?? Commented Jul 11, 2018 at 12:19
  • 2
    Range function default step is 1. So the error comes. Commented Jul 11, 2018 at 12:19
  • 1
    Let me add another answer first with the same result! Commented Jul 11, 2018 at 12:49

5 Answers 5

8

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);
Sign up to request clarification or add additional context in comments.

Comments

3

You should also provide the range you want.

Like this

var_dump(range(0.54,0.58,0.01));

output is

array(4) {
  [0]=>
  float(0.54)
  [1]=>
  float(0.55)
  [2]=>
  float(0.56)
  [3]=>
  float(0.57)
}

Comments

3

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

1 Comment

Thanks for pointing this out, so I was missing the increment property of the function.
1

use the third parameter to set a step

range(0.54,0.58, 0.01);

or you could multiply your values by 1000 and then divide by 1000 which would be silly...

Comments

0

The other answers here did not work for me as, due to rounding error with floats, the result is not exact:

  • Depending on the start and end numbers the result is inclusive or exclusive the end number.
  • Additionally in PHP 8 the rounding errors are visible in the result. (Seams to only apply to debug output.)

Examples:

php 7.4: (inclusive vs. exclusive)

php > var_dump(range(0.54, 0.6, 0.01));  
array(6) {
  [0]=>
  float(0.54)
  [1]=>
  float(0.55)
  [2]=>
  float(0.56)
  [3]=>
  float(0.57)
  [4]=>
  float(0.58)
  [5]=>
  float(0.59)
}
php > var_dump(range(0.52, 0.6, 0.01)); 
array(9) {
  [0]=>
  float(0.52)
  [1]=>
  float(0.53)
  [2]=>
  float(0.54)
  [3]=>
  float(0.55)
  [4]=>
  float(0.56)
  [5]=>
  float(0.57)
  [6]=>
  float(0.58)
  [7]=>
  float(0.59)
  [8]=>
  float(0.6)
}

php 8.0: (inclusive vs. exclusive + rounding errors)

php > var_dump(range(0.54, 0.6, 0.01));
array(6) {
  [0]=>
  float(0.54)
  [1]=>
  float(0.55)
  [2]=>
  float(0.56)
  [3]=>
  float(0.5700000000000001)
  [4]=>
  float(0.5800000000000001)
  [5]=>
  float(0.5900000000000001)
}
php > var_dump(range(0.52, 0.6, 0.01));
array(9) {
  [0]=>
  float(0.52)
  [1]=>
  float(0.53)
  [2]=>
  float(0.54)
  [3]=>
  float(0.55)
  [4]=>
  float(0.56)
  [5]=>
  float(0.5700000000000001)
  [6]=>
  float(0.5800000000000001)
  [7]=>
  float(0.5900000000000001)
  [8]=>
  float(0.6)
}

Solution:

This does give my expected result (PHP 7.4 and up):

php > var_dump(array_map(fn($n) => $n/100, range(0.54*100, 0.6*100,  0.01*100)));
array(7) {
  [0]=>
  float(0.54)
  [1]=>
  float(0.55)
  [2]=>
  float(0.56)
  [3]=>
  float(0.57)
  [4]=>
  float(0.58)
  [5]=>
  float(0.59)
  [6]=>
  float(0.6)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.