# operator strange behaviour

- Thread: https://externals.io/message/13634
- Original archive: https://news-web.php.net/php.internals/13634
- Started by Hodicska Gergely on 2004-10-30T18:45:07+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #13634 — Hodicska Gergely — 2004-10-30T18:45:07+00:00

URL: https://externals.io/message/13634#13634

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

---

## #13635 — Derick Rethans — 2004-10-30T18:58:38+00:00

URL: https://externals.io/message/13634#13635 · In reply to: #13634

> 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

---

## #13636 — Robert Cummings — 2004-10-30T19:10:41+00:00

URL: https://externals.io/message/13634#13636 · In reply to: #13634

> 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.          |
`------------------------------------------------------------'

---

## #13637 — Hodicska Gergely — 2004-10-30T19:15:37+00:00

URL: https://externals.io/message/13634#13637 · In reply to: #13635

> 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

---

## #13638 — Derick Rethans — 2004-10-30T19:17:33+00:00

URL: https://externals.io/message/13634#13638 · In reply to: #13637

> > […]
> 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
