Page 1 of 1

VERY strange variable value check [solved]

Posted: Wed Jun 15, 2005 12:37 am
by robster
Hello all,

I have a long piece of code that works through a bunch of problems, and if a problem is correct, it assigns the variable $pass to true, if it's incorrect it assigns it to false.

The idea being at the end of the script, we know if the user passed or not (true or false).

I'll post a snippet here, as well as some output, and I tell you what, I'm baffled.
The snippit:

Code: Select all

//If all checks above went badly, then let the user know what they did wrong
		/* ********************************************************************** */

		echo "<br><br>pass is now finally set to $pass<br><br>";


		if ($pass = "false")
		{
			echo "<table width=\"50%\" border=\"0\" cellpadding=\"5\" cellspacing=\"10\" align=\"center\"><tr><td class=\"error_box\" align=\"center\">";
			echo "Attention!<br>You have not answered all the questions, please scroll down, look for the red errors and fill in those questions.<br>Thank you.";
			echo "</td></tr></table>";
		}
		//Else let the user know they did well (or at least just process the info 
		elseif ($pass = "true")
		{
			echo "All checks passed, you can continue...<br>";
		}
and the output:

Code: Select all

pass is now finally set to true

Attention!
You have not answered all the questions, please scroll down, look for the red errors and fill in those questions.
Thank you.
Now excuse me here ;) but if $pass is set to true (as stated in the check before the if then else, then why oh why is it executing the if pass = false code?!

I'm... going mad I think ;)

Perhaps some fresh eyes?

Posted: Wed Jun 15, 2005 12:39 am
by Syranide
because you use the assignement operator =.
not the comparion operator ==.

meaning that what ever you check, will be assigned, and unless that is empty, NULL or so it will be true. and as "false" comes first, that becomes true.

Posted: Wed Jun 15, 2005 12:41 am
by robster
ahem (cough)

I'll just ... wander over here, in this dark corner, and hide

:roll:


:D

Thank you (shakes his head)

Posted: Wed Jun 15, 2005 4:20 am
by vigge89
Just a tip, don't set $pass to "false" if it was invalid, use $pass = false; or something, what if the user want the pass to be 'false'?

Posted: Wed Jun 15, 2005 4:44 am
by robster
a bit confused, what's the difference between setting $pass to false and $pass = false; ?

Posted: Wed Jun 15, 2005 4:48 am
by Syranide
'false' = string (equals true when tested)
false = boolean (integer representation is 0)

EDIT: ah, he meant you set it to 'false' and not false.

Posted: Wed Jun 15, 2005 5:03 am
by robster
ahhh, I am doing the ol' $pass = "false" (string stlyin')