Re: Boolean type forced on string assignment inside if statement

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Jan 2, 2013 at 1:02 PM, Marc Guay <marc.guay@xxxxxxxxx> wrote:
> Something else that's happening with this, which makes it a Bad Idea
> (tm) is that when the operator is "or", as it is in my real life
> scenerio, the 2nd variable occasionally doesn't get populated if the
> first one returns true.
>
> if ($a = "foo" || $b = "bar"){
>     echo $a."<br />".$b;
> }
>
> Returns
> foo
>
> And even worse, because I have this in a loop, what can happen is that
> if $b gets populated on one loop, it doesn't get reset for the next
> one so the data gets seriously bungled.

Ah, yeah, seriously, don't mix assignments and tests up this way
unless it doesn't matter that the second part never gets set. It's
called "short-circuiting", and nearly every language (the ones I can
think of) does it.

This may seem DRY, but it's not. It's merely obfuscating, if not
completely incorrect for what you want.

Under which conditions will $a = "foo" be false? Even if you do $a =
'' or $a = 0, these might not be failing conditions, depending.

> Moral of the story:  Don't be so fancy on your first day back after
> vacation.  :)

GIve it a week, at least :)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux