See PHP Users Group Meeting Last Night at its new home on bradley-holt.com.
The Burlington, VT PHP Users Group met last night. Thank you Bluehouse Group for hosting and providing pizza and soda! Rob Riggen gave a talk on PHP Frameworks: 3 different ones and we went around the room talking about a PHP function or tip that helped us in the past. I decided to talk about the following very simple tip.How many times have you done something like this:
if ($foo = 1) { }
when you meant to do this:
if ($foo == 1) { }
This is a common mistake to see. A developer accidentally uses the single equal assignment operator instead of the double equals comparison operator. Since PHP will successfully assign the value it will always return true which was not the intended result. A simple way to catch this mistake is to reverse the variable and the literal value:
if (1 == $foo) { }
This still has the intended effect of comparing the two values but if you accidentally use a single equal sign:
if (1 = $foo) { }
then you will get a parse error. Here we've introduced the concept of fail-fast into our code. We'd rather get a fatal error letting us know we made a typo then a potentially insidious and hard-to-find logic error.
No comments:
Post a Comment