Posted by David Haynes on 03/21/06 12:51
Jim Michaels wrote:
> This is a simple coding tip I learned to prevent comparisons from becoming
> assignments (and this becoming a hair-pulling session during debugging):
>
> in comparisons, try to put your constants on the left hand side of the
> operator and variables on the right hand side.
> example
> if (3==$x) {
> //do something
> }
>
>
>
> if ($x=3) {
> //do something
> }
> if this were intended as a comparison, it would never be flagged as an error
> by the interpreter or compiler (in PHP's case, interpreter).
> With this line, I missed an = and the interpreter won't complain. it simply
> assigns 3 to $x and does something every time. With the top line, you can
> fix your code quickly because of the error message generated if you miss an
> =.
> Consider it a good coding habit? Then again there are books on this (if
> they are still published).
>
>
One issue is that it only works if the left-hand side is a constant.
-david-
[Back to original message]
|