wildmurphy Posted October 28, 2011 Share Posted October 28, 2011 Hi Guys, I am having an issue with an if statement, I cant get it to go true, even though it clearly is! Please excuse the messy code, im pulling my hair out here! $itemprice[0] = 8.99; $itemprice[1] = 19.95; $itemprice[2] = 8.99; print_r($itemprice); echo "<BR>"; $x=0; foreach($itemprice AS $val) { echo gettype($val) .$val. "<br>"; if($val == 8.99) { $x++; $freecount++; } } echo $x; outputs correctly: Array ( [0] => 8.99 [1] => 19.95 [2] => 8.99 ) double8.99 double19.95 double8.99 x = 2 replacing: $newtotal = add_to_price($id, $non_disc); $itemprice[] = $newtotal - $non_disc; print_r($itemprice); echo "<BR>"; $x=0; foreach($itemprice AS $val) { echo gettype($val) .$val. "<br>"; if($val == 8.99) { $x++; $freecount++; } } echo $x; The add to price function returns a running total, the take away gives me the current item price in a for loop to build the array. Output: Array ( [0] => 8.99 [1] => 19.95 [2] => 8.99 ) double8.99 double19.95 double8.99 x = 1 The problem? Well when I build the array up automatically it still prints the same, has the same datatype, yet the if statement does not catch the second 8.99 value? Please accept my apologies for this post, it is my first ever and i've been doing this for 10 years now, i have never been this stumped on something so simple, I can only think it is a datatype error but i have tried apostrophes in the if, make no difference. All help massively gratefully received. Please email me if you want to see it in action... Quote Link to comment https://forums.phpfreaks.com/topic/249968-problem-with-if/ Share on other sites More sharing options...
trq Posted October 28, 2011 Share Posted October 28, 2011 You know that programmers generally indent there code so that it is readable? Quote Link to comment https://forums.phpfreaks.com/topic/249968-problem-with-if/#findComment-1282967 Share on other sites More sharing options...
wildmurphy Posted October 28, 2011 Author Share Posted October 28, 2011 Thanks thorpe, yes i do thanks. Please accept my apologies for this non indent on the forum, my indents looked a bit wide so i tried to smarten it up on here and it went wrong. The problem as it turned out was that the datatype coming back from my fuction was changing and this was not carried through to the array. Even though they look the same before the if test when print_r and gettype were echoed they apparently were not. Not something I have ever experienced in php, its usually good at sorting out its own datatypes. Quote Link to comment https://forums.phpfreaks.com/topic/249968-problem-with-if/#findComment-1282978 Share on other sites More sharing options...
The Little Guy Posted October 28, 2011 Share Posted October 28, 2011 be careful when using floats, you should round them because some floats are off by just a tiny bit. Read (short): http://edn.embarcadero.com/article/22507 You should round your floating point values to 2 decimal points using round especially when comparing two values. Quote Link to comment https://forums.phpfreaks.com/topic/249968-problem-with-if/#findComment-1283096 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.