RE: [PHP] Alternate Colors in Rows ($r=!$r)
| From: | Robert Cummings | Date: | Fri, 05 Oct 2007 19:54:42 +0000 |
| Subject: | RE: [PHP] Alternate Colors in Rows ($r=!$r) | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-262848@lists.php.net to get a copy of this message | ||
On Fri, 2007-10-05 at 14:17 -0500, Jay Blanchard wrote:
> [snip]
> if($r = !$r)
> [/snip]
>
> And I hit send before I finished my thought process oh my goodness isn't
> it five o'clock yet and why do all of these people keep coming by my
> office distracting me from getting something useful done like replying
> the PHP list and why doesn't someone bring me a beer?
>
> if($r = !$r) is a conditional check that most folks would read as
> follows;
>
> if the assignment of $r to !$r occurs the statement is TRUE. Since PHP
> is loosely typed we all know that an assignment will occur regardless of
> what is assigned. That why we use additional operators to determine the
> TRUEness of a statement;
>
> if($r == !$r) or if($r === !$r)
>
> That is why we code conditional checks (if we're smart) by putting the
> constant on the left hand side of the check to reduce/locate
> typographical errors when coding conditional checks; if(1 == $foo),
> because if we assign $foo to 1 the compiler will throw an error and we
> can fix it pretty quickly.
I never do that. It's unnatural to read. But others get good mileage out
of it.
> This is one of those special cases where this logic gets thrown out. I
> am going to see if the same thing will work in C++ because I am pretty
> sure that no other language has this feature....maybe C.
It is legal in C, C++, Java, JavaScript, SmallTalk, probably almost
every other language out there. However, convention I believe is to
place an extra set of parenthesis around the assignment for clarity. In
fact, many C compilers will spout a warning if you don't put the extra
set of parenthesis around the assignment.
Example:
if( ($r = !$r) )
Cheers,
Rob.
--
...........................................................
SwarmBuy.com - http://www.swarmbuy.com
Leveraging the buying power of the masses!
...........................................................