0

An if condition in a for loop would stop working after (n) times.

I'm using PHP 7.3.6-1+ubuntu18.04.1+deb.sury.org+1

Given the following code:

for($i = 0.99; $i<= 30.99; $i++){
    echo $i;
    if($i == 10.99){
        echo '<--- selected';
    }
    echo '<br>';
}

output

0.99
1.99
2.99
3.99
4.99
5.99
6.99
7.99
8.99
9.99
10.99<--- selected
11.99
12.99
13.99
14.99
15.99
16.99
17.99
18.99
19.99
20.99
21.99
22.99
23.99
24.99
25.99
26.99
27.99
28.99
29.99

This remain true when the condition is between 0.99 - 15.99, However, changing the condition between 16.99 - 29.99, <--- selected is not returned.

I ran few tests using int in my loop as shown below and it seems to work fine.

for($i = 0; $i<= 30; $i++){
    echo $i;
    if($i == 18){
        echo '<--- selected';
    }
    echo '<br>';
}

I think the problem is related to this float (0.99 - 30.99).

ForALLs
  • 27
  • 5

2 Answers2

0

Let's clean the float before we start the comparison.

<?php
for($i = 0.99; $i<= 30.99; $i++){
    $i = number_format((float)$i, 2);
    echo $i;
    if($i == 16.99){
        echo '<--- selected';
    }
    if($i == 29.99){
        echo '<--- selected';
    }
    if($i == 10.99){
        echo '<--- selected';
    }
    echo '<br>';
}
LIGHT
  • 5,604
  • 10
  • 35
  • 78
  • I tried this and it worked. My question though is, why did it return true between `0.99 to 15.99` and false between `16.99 - 30.99` – ForALLs Jul 07 '19 at 19:23
  • https://stackoverflow.com/questions/588004/is-floating-point-math-broken – LIGHT Jul 07 '19 at 19:31
0

A possible solution is to make the numbers string and that way compare them.
Using "10.99" on one side and number_format on the other

for($i = 0.99; $i<= 30.99; $i++){
    echo $i;
    if(number_format($i,2) == "22.99"){
        echo '<--- selected';
    }
    echo "\n";
}

https://3v4l.org/mvOR3

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • We could just use regular float comparison instead of string after we format it to 2nd decimal place, no? check my answer, would you – LIGHT Jul 07 '19 at 19:17
  • I have tried it and it worked. I'll accept this as my answer for simplicity, but could you explain this: why did it return true between `0.99 to 15.99` and false between `16.99 - 30.99` while I was testing. – ForALLs Jul 07 '19 at 19:30
  • @ForALLs a float is not precise. Most likely it will not be the same if you use 0.999. see the link I posted above in comments, also there are other articles and YouTube videos explaining it. – Andreas Jul 07 '19 at 19:48