Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:13636 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 5005 invoked by uid 1010); 30 Oct 2004 19:09:20 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 4958 invoked from network); 30 Oct 2004 19:09:20 -0000 Received: from unknown (HELO blobule.suds) (66.11.170.154) by pb1.pair.com with SMTP; 30 Oct 2004 19:09:20 -0000 Received: by blobule.suds (Postfix, from userid 501) id 5BEDB131BC4; Sat, 30 Oct 2004 15:10:41 -0400 (EDT) To: Hodicska Gergely Cc: internals@lists.php.net In-Reply-To: <4183E133.1050103@avalon.aut.bme.hu> References: <4183E133.1050103@avalon.aut.bme.hu> Content-Type: text/plain Content-Transfer-Encoding: 7bit Organization: InterJinn Message-ID: <1099163441.12038.15.camel@blobule.suds> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.5-4mdk Date: Sat, 30 Oct 2004 15:10:41 -0400 Subject: Re: [PHP-DEV] operator strange behaviour From: robert@interjinn.com (Robert Cummings) 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. | `------------------------------------------------------------'