VERY strange variable value check [solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

VERY strange variable value check [solved]

Post 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?
Last edited by robster on Wed Jun 15, 2005 12:42 am, edited 1 time in total.
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post 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.
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

ahem (cough)

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

:roll:


:D

Thank you (shakes his head)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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'?
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

a bit confused, what's the difference between setting $pass to false and $pass = false; ?
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post 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.
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

ahhh, I am doing the ol' $pass = "false" (string stlyin')
Post Reply