You are here: Re: coding tip to prevent comparisons becoming assignments « PHP Programming Language « IT news, forums, messages
Re: coding tip to prevent comparisons becoming assignments

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-

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация