On Thursday 03 January 2013 10:26:39 Jim Giner wrote:
> On 1/2/2013 2:02 PM, Marc Guay 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.
> > 
> > Moral of the story:  Don't be so fancy on your first day back after
> > vacation.  :)
> > 
> > Marc
> 
> You actually use statements like that in order to populate vars?
> Whatever happened to "simple to understand, easy to maintain" coding
> practices?
> 
> The only time I use a single '=' symbol in an if statement is when I
> forget to use two of them!  Must be my old school, old languages habits
> but this style of programming reminds me of the days when we used to
> pack empty spaces in assembler code with constants or byte-size vars in
> order to save memory back when memory was the most precious resource one
> had.

I have been watching this discussion with some amusement and I recall the 
days mentioned by Jim very well indeed!

First, did the original poster realize that he was assigning a value to the 
variable $a in the 'if' statement?  Assuming that he did, and this is not 
just a typo, then remember how the if statement evaluates an OR condition; 
that is, if the first variable is 'true' then the true path is followed 
because there is no reason to go further.  So the result is EXACTLY what 
one would expect.  $a is true (ie it is not set to a 'false' value, 
whatever PHP uses for false) and $b is "bar" because that is what it is set 
to.  Since the evaluation is within a bracket, the interior values ($a, $b) 
are set BEFORE the if condition is evaluated.

I see neither a bug in PHP nor a variance from the expected result here.

Regards,

John

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

Reply via email to