PHP Debugging – “=” vs “==”

I spent about 3 hours debugging my php code all over a missing “=” !!!

One “=” is a variable assignment, and two “==” is a test condition. Something like…

$var = false;   // a boolean variable, assign a value of false

if ($var == true) { do something } ;

But what I was doing instead was …

if ($var = true) { do something } ;  // Ie, one “=”

The affect of doing this is the variable $var is being assigned the value of “true”. PHP doesn’t give you an error or a warning even though it actually doesn’t make sense to do an assign value in a conditional test!! So I spent 3 hours in frustration trying to figure out where in my code my variable is mysteriously being changed back to true when it should be false ! Aaargghhh.

End of vent.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.