sam06 Posted February 2, 2011 Share Posted February 2, 2011 Absolutely no idea why my IF statement's playing up - needs some fresh eyes I think! if ($postcheck != $post){ if ($x == "0"){ echo $title; echo "<br>"; $x = "1"; $y = $y + 1; } echo "Change Post to: "; echo $postcheck; echo " from "; echo $post; echo "<br>"; $result2 = mysql_query("UPDATE items SET Post='$postcheck' WHERE ID='$id'") or die(mysql_error()); } When it returns $postcheck and $post, in this case, they're coming out as the same thing (on one record - no others!) - $post is retrieved from a database, and $postcheck is calculated. Both are numbers. Any ideas?! Quote Link to comment https://forums.phpfreaks.com/topic/226497-why-is-my-if-statement-not-working/ Share on other sites More sharing options...
coupe-r Posted February 2, 2011 Share Posted February 2, 2011 Before your IF(), echo both vars on the screen so you can see what is being compared. *** Nevermind, was looking at something else. Quote Link to comment https://forums.phpfreaks.com/topic/226497-why-is-my-if-statement-not-working/#findComment-1169055 Share on other sites More sharing options...
btherl Posted February 2, 2011 Share Posted February 2, 2011 Can you also tell us what you expected to see and what you actually saw. I'm not clear on whether the "if" succeeded when you thought it wouldn't, or it failed when you thought it would succeed. Quote Link to comment https://forums.phpfreaks.com/topic/226497-why-is-my-if-statement-not-working/#findComment-1169065 Share on other sites More sharing options...
sam06 Posted February 2, 2011 Author Share Posted February 2, 2011 Running: echo $postcheck; echo "<br>"; echo $post; echo "<br>"; before the if statement gives me: 1.89 1.89 Example Item 1 Change Post to: 1.89 from 1.89 The 1.89 and 1.89 look the same to me, so no idea why it's running the != statement...?! Should expect no output Quote Link to comment https://forums.phpfreaks.com/topic/226497-why-is-my-if-statement-not-working/#findComment-1169067 Share on other sites More sharing options...
coupe-r Posted February 2, 2011 Share Posted February 2, 2011 Can you show how you are getting those vars? ie. $post = $row['post'];?? Also, how are they stored in the DB? (datatype) Quote Link to comment https://forums.phpfreaks.com/topic/226497-why-is-my-if-statement-not-working/#findComment-1169076 Share on other sites More sharing options...
Psycho Posted February 2, 2011 Share Posted February 2, 2011 I would also suggest changing those echo statements to something like this echo "Postcheck: [$postcheck]<br>"; echo "Post: [$post]<br>"; It could be something as simple as a leading/trailing space, which the brackets should make apparent. You should also view the HTML source code to see if there is anything different. Quote Link to comment https://forums.phpfreaks.com/topic/226497-why-is-my-if-statement-not-working/#findComment-1169083 Share on other sites More sharing options...
btherl Posted February 2, 2011 Share Posted February 2, 2011 Looks like a trailing space will cause them to compare differently, but not a leading space: <? $a = '1.89'; $b = ' 1.89'; if ($a != $b) print "[$a] != [$b]\n"; $b = '1.89 '; if ($a != $b) print "[$a] != [$b]\n"; ?> Print out urlencode($post) is also a good way of finding any hidden characters. Quote Link to comment https://forums.phpfreaks.com/topic/226497-why-is-my-if-statement-not-working/#findComment-1169102 Share on other sites More sharing options...
sam06 Posted February 3, 2011 Author Share Posted February 3, 2011 Just changed the datatype in the DB to decimal. Now is throwing up the same error in another record! Using the brackets echo from above I now get: Postcheck: [1.99] Post: [1.99] Postcheck: [1.19] Post: [1.19] Example Item 2 Change Post to: 1.19 from 1.19 Postcheck: [1.49] Post: [1.49] Quote Link to comment https://forums.phpfreaks.com/topic/226497-why-is-my-if-statement-not-working/#findComment-1169291 Share on other sites More sharing options...
btherl Posted February 3, 2011 Share Posted February 3, 2011 Well that is odd indeed. Can you try printing the values like this: echo "Postcheck: [" . urlencode($postcheck) . "]<br>"; echo "Post: [" . urlencode($post) . "]<br>"; If that shows them to be identical, try this: echo "<pre>"; echo "Postcheck: "; var_dump($postcheck); echo "Post: "; var_dump($post); One of those should show up why they look the same but are unequal according to php. Quote Link to comment https://forums.phpfreaks.com/topic/226497-why-is-my-if-statement-not-working/#findComment-1169576 Share on other sites More sharing options...
sam06 Posted February 4, 2011 Author Share Posted February 4, 2011 I've changed it to reverse - if theyre the same, do nothing, else this - and that works! It returns: float 1.99 Post: string '1.99' (length=4) Postcheck: float 1.19 Post: string '1.19' (length=4) Postcheck: float 1.49 Post: string '1.49' (length=4) Quote Link to comment https://forums.phpfreaks.com/topic/226497-why-is-my-if-statement-not-working/#findComment-1169982 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.