Reply to Re: coding tip to prevent comparisons becoming assignments

Your name:

Reply:


Posted by Ian Collins on 03/21/06 08:32

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?

It's required in some shops. In others, it's a matter of taste. Decent
unit tests also help prevent this problem.

--
Ian Collins.

[Back to original 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

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