Jump to content

Numeric Comparing giving wrong answer


Lokolo

Recommended Posts

	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?

Link to comment
https://forums.phpfreaks.com/topic/50679-numeric-comparing-giving-wrong-answer/
Share on other sites

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!

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.