0

When I run this code:

$value = log(1000, 100);

echo $value;

if ($value == 1.5) {
    echo 'Equal';
} else {
    echo 'Not Equal';
}

I see 1.5Not Equal. This is very strange because log(1000, 100) does return 1.5, but it does not match the if statement.

Why does PHP do this?

  • Does this answer your question? [Compare floats in php](https://stackoverflow.com/questions/3148937/compare-floats-in-php) – Gaurav Feb 25 '22 at 11:36

1 Answers1

0

If you var_dump your $value you will see the result is float(1.4999999999999998) and that is not equal to 1.5, you need to use round like:

$value = round(log(1000, 100), 1);

if ($value == 1.5) {
    echo 'Equal';
} else {
    echo 'Not Equal';
}

Here you can see result.

Reference:

Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34