Lokolo Posted May 9, 2007 Share Posted May 9, 2007 echo "$_SESSION[totalcost] <br>"; echo "$_SESSION[funds] <br>"; IF ( $_SESSION[funds] < $_SESSION[totalcost] ) { echo "You do not have enough cash to upgrade <br>"; } ELSE { echo "Upgrading... <br>"; } Output: 75,000 622,500 You do not have enough cash to upgrade I have no idea why. What reasons could it be? Quote Link to comment https://forums.phpfreaks.com/topic/50679-numeric-comparing-giving-wrong-answer/ Share on other sites More sharing options...
per1os Posted May 9, 2007 Share Posted May 9, 2007 It should work given that but maybe try something like this? <?php if (($_SESSION['funds'] - $_SESSION['totalcost']) < 0) { echo "Insufficient Funds"; }else { echo "Purchased"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50679-numeric-comparing-giving-wrong-answer/#findComment-249109 Share on other sites More sharing options...
Lokolo Posted May 9, 2007 Author Share Posted May 9, 2007 ok will try that. however, i got it to work by removing the echo "$_SESSION[totalcost] <br>"; echo "$_SESSION[funds] <br>"; statements, as a PHP Expert (you seem to be answering everyones questions with ease) why could this be?! [edit] your way worked. hm i deleted a few statements and it didnt work again, so used yours, and harruh! Quote Link to comment https://forums.phpfreaks.com/topic/50679-numeric-comparing-giving-wrong-answer/#findComment-249115 Share on other sites More sharing options...
per1os Posted May 9, 2007 Share Posted May 9, 2007 Chances are it could be a notice conflict. Because you are essentially using constants to reference the index of the array. I would highly suggest you steer away from that and echo it out like so: <?php echo $_SESSION['totalcost'] ." <br>"; echo $_SESSION['funds'] . " <br>"; ?> The way above will overall be more efficient and correct. This way no notice is thrown for using an undefined constant. Essentially what happens right now, if you user funds without single quotes to try and grab an object at that index of the array the code first searchs to check to see if there is a constant called "funds" if not than it just assumes the word. Anyhow maybe give that a test and see if that works. Quote Link to comment https://forums.phpfreaks.com/topic/50679-numeric-comparing-giving-wrong-answer/#findComment-249136 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.