Jump to content

[SOLVED] Exit script diffent outcome when change variable in if statement ?


stevem123uk

Recommended Posts

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");

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.