RE: [PHP-DEV] Re: power operator (again)

From: Date: Sat, 21 Dec 2013 10:29:00 +0000
Subject: RE: [PHP-DEV] Re: power operator (again)
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-70809@lists.php.net to get a copy of this message
> -----Original Message----- > From: Rowan Collins [mailto:rowan.collins@gmail.com] > Sent: Friday, December 20, 2013 10:28 AM > To: internals@lists.php.net > Subject: Re: [PHP-DEV] Re: power operator (again) > > Kris Craig wrote (on 20/12/2013): > > But now, let's look at another scenario: > > > > <?php > > > > $x = -3; // Or $x = -1 * 3, if you wish. > > > > $y = $x ** 2; > > > > ?> > > > > What would be the value of $y under this RFC? > > That has nothing to do with operator associativity: $x has already been > calculated as -3, just as if it were $y = (-3) ** 2 > That's actually wrong. Consider the following code snippet: echo ( -3 * 2); // -6 $x = -3; echo $x * 2; // -6 -> does not behave like (-3) * 2 $x = "-3"; echo $x * 2; // -6 -> does not behave like (-3) * 2 But I think it would be more intuitive if it would behave as (-3) > Consider the same thing with mixed addition and multiplication: > > $y = 1 + 2 * 3; > // $y is 7, due to operator precedence > > $y = (1 + 2) * 3; > // $y is 9, due to the brackets > > $x = 1 + 2; > $y = $x * 3; > // $x is 3, so $y is 9; operator precedence is irrelevant > Nope, that's not true. 1+2 is just already evaluated and the result was stored in $x rather than the expression. > As I said before, I think people don't always think of "-3" as an > operator applied to a literal, but by the same token, they are rather > unlikely to write something like "-$x". That limits this whole debate on > associativity to those cases where somebody hard-codes a negative value > like -3, and doesn't think to add parentheses. > > Regards, > -- > Rowan Collins > [IMSoP] > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.internals (#70809) next »