operator strange behaviour

php.internals

Hodicska Gergely

21 years ago
Hi! I tried to get answer on the general list, but I didn't get. I have a little example, which generate strange output. $a = 0; $b = 1; if ($a = 1 && $b = 0) { echo 'true '; var_dump($a); var_dump($b); } else { echo 'false '; var_dump($a); var_dump($b); } Runing this we get: "flase bool(false) int(0)" After the precedence table the first step should be evaluating the &&, and we get something like this: $a = false = 0, which should generate an error, while there isn't an "lvalue" on the left side of the =. But doesn't this happen. It seems, that the evaluation of the first = came before evaluating the &&. Can someone exactly explain how PHP process this condition? THX in advance, Felho

Derick Rethans

21 years ago
On Sat, 30 Oct 2004, Hodicska Gergely wrote:
> I tried to get answer on the general list, but I didn't get. > I have a little example, which generate strange output.
This is the wrong list, see http://php.net/support Derick
-- Derick Rethans http://derickrethans.nl | http://ez.no | http://xdebug.org

Hodicska Gergely

21 years ago
> This is the wrong list, see http://php.net/support
Oke, which one do you suggest? I thought that the developer of the language could answer this question. Felho

Derick Rethans

21 years ago
On Sat, 30 Oct 2004, Hodicska Gergely wrote:
> > This is the wrong list, see http://php.net/support > Oke, which one do you suggest? I thought that the developer of the > language could answer this question.
We can, but you still should ask on the php-general@lists.php.net mailinglist. Derick
-- Derick Rethans http://derickrethans.nl | http://ez.no | http://xdebug.org

Robert Cummings

21 years ago
On Sat, 2004-10-30 at 14:45, Hodicska Gergely wrote:
> Hi! > > I tried to get answer on the general list, but I didn't get. > I have a little example, which generate strange output. > > $a = 0; > $b = 1; > if ($a = 1 && $b = 0) { > echo 'true '; > var_dump($a); > var_dump($b); > } else { > echo 'false '; > var_dump($a); > var_dump($b); > } > Runing this we get: "flase bool(false) int(0)" > > After the precedence table the first step should be evaluating the &&, > and we get something like this: $a = false = 0, which should generate an > error, while there isn't an "lvalue" on the left side of the =. > But doesn't this happen. It seems, that the evaluation of the first = > came before evaluating the &&. > > Can someone exactly explain how PHP process this condition?
You are mixing right and left associativity operators without parenthesis. The PHP doc at: http://www.php.net/manual/en/language.operators.php Clearly states that you can do !$a = foo() where the result of foo() will be assigned to $a. Thus in your example the order of evaluation is: evaluate $b = 0 evaluate the result of 1 && $b assign the previous evaluation result to $a Thus: $b = 0 1 && 0 = false $a = false HTH, Rob.
-- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'