Re: logical AND assignments

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

 



On Tue, Sep 07, 2010 at 10:41:46PM -0400, Robert E. Glaser wrote:

> My ISP upgraded his server from Ubuntu 9.04 to Ubuntu 9.10, which
> probably included a newer PHP version.  I don't know what PHP version
> was on previously.  Code I've had running for years broke, and I tracked
> it down to this equivalent:
> 
> <?php
> 
> echo 'Current PHP version: ', phpversion(), "<br><br>";
> 
> $Condition0 = true and false;
> If ($Condition0)
> 	echo "Condition0<br>";
> 	else echo "Not Condition0<br>";
> 
> $Condition1 = false and true;
> If ($Condition1)
> 	echo "Condition1<br>";
> 	else echo "Not Condition1<br>";
> 
> $Condition2 = (true and false);
> If ($Condition2)
> 	echo "Condition2<br>";
> 	else echo "Not Condition2<br>";
> 
> $Condition3 = true && false;
> If ($Condition3)
> 	echo "Condition3<br>";
> 	else echo "Not Condition3<br>";
> 
> $Condition4 = (true && false);
> If ($Condition4)
> 	echo "Condition4<br>";
> 	else echo "Not Condition4<br>";
> ?>
> 
> which returns:
> 
> Current PHP version: 5.2.10-2ubuntu6.4
> 
> Condition0
> Not Condition1
> Not Condition2
> Not Condition3
> Not Condition4
> 
> ===============================
> 
> I added parentheses around the offending line of code and it seems okay
> now.  But I am stymied as to why they're required at all.  They never
> were before, and as far as I see there is no PHP requirement to include
> the parentheses.  Have I done something silly?	Mostly I'm worried if
> there are any other changes I need to make.  I searched my code and
> hopefully found all similar instances, but who really knows about those
> kinds of things?
> 
> Any comments?

I can't tell what (if anything) changed or why. But the problem appears
to rest with the fact that "&&" is higher precedence than "=", and "and"
is lower precedence. Here are two references:

http://us.php.net/manual/en/language.operators.logical.php

http://us.php.net/manual/en/language.operators.precedence.php

It's tricky, I'll admit. Since I normally mean for things I'm logically
"anding" to be "anded" before any other operation, it's forced me to
change almost all my "ands" to "&&". Just so I don't make a mistake.

Also note the "associativity" in the second reference.

Paul

-- 
Paul M. Foster

-- 
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