stevem123uk Posted January 31, 2008 Share Posted January 31, 2008 Hi Need some help with the script below. I can not understand why when i change the $check1 variable to $check in the if statement the outcome is different. Any help would be appriciated. <?php $lastcheck = 1.4; $check1 = $lastcheck; $check1 = $check1 + .7 ; $check = 2.1; $dt = 2.1; if ($dt == $check1) die ("Stop and display this.<br>dt = $dt<br>lastcheck = $lastcheck<br>check = $check<br>check1 = $check1"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/88753-solved-exit-script-diffent-outcome-when-change-variable-in-if-statement/ Share on other sites More sharing options...
Barand Posted February 1, 2008 Share Posted February 1, 2008 The problem with float type is small errors always creep in during calculations. When using floats, don't rely on the "==" operator. Instead you need to check if the difference is tolerably small, so you need something like if (abs($dt - $check) < 0.00000001) I added a line to your code before your if() if ($check != $check1) die ($check - $check1); // --> 4.4408920985E-016 Quote Link to comment https://forums.phpfreaks.com/topic/88753-solved-exit-script-diffent-outcome-when-change-variable-in-if-statement/#findComment-454867 Share on other sites More sharing options...
stevem123uk Posted February 1, 2008 Author Share Posted February 1, 2008 Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/88753-solved-exit-script-diffent-outcome-when-change-variable-in-if-statement/#findComment-455568 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.