FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Operator precedence
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Operator precedence [message #185018 is a reply to message #185005] Mon, 24 February 2014 13:28 Go to previous message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma:
Senior Member
Ben Bacarisse wrote:

> Christoph Michael Becker <cmbecker69(at)arcor(dot)de> writes:
>
>> I've stumbled upon the following code:
>>
>> !$a && $a = 42;
>>
>> Apparently, this is meant instead of
>>
>> if (!$a) $a = 42;
>>
>> or
>>
>> !$a AND $a = 42;
>>
>> However, I am quite confused that it works because the = operator has
>> lower precendence than the && operator[1]. Actually that should mean
>> the statement is equivalent to
>>
>> (!$a && $a) = 42;
>>
>> But this obviously won't work.
>
> The precedence table is wrong -- or at least something of a
> simplification. The grammar for PHP (for which I had to resort to the
> source code) does not reflect that table for the = operator. The
> grammar has explicit rules for = that result in the precedence being
> ignored in this particular case.
>
> The precedence comes into play when the YACC (or Bison or whatever)
> source says something like
>
> exp: ...
> | exp '=' exp
> | ...
>
> but instead is says:
>
> exp: ...
> | variable = exp
>
> (this is a somewhat abbreviated explanation) so there can never be a
> full expression on the left hand side. Note that this works:
>
> $a + $a = 42;
>
> and parses like
>
> $a + ($a = 42);
>
> There is nothing special about && in this respect. The precedence does
> count in some situations. For example, compare
>
> $a = true && $b = false; // $a = (true && ($b = false));
>
> and
>
> $a = true and $b = false; // ($a = true) and ($b = false);

Thanks for your investigations and explanations which confirm my later
findings.

>> Why does the mentioned statement has the "expected" result? Is it
>> reliable to write it that way?
>
> Interesting question. I don't know where the language is defined.
> Maybe one day the source code will reflect the documentation of vice
> versa. Maybe one day both will match some other, external,
> specification.

Indeed, it seems that a formal syntax *specification* is missing,
especially because the expression grammar has striking similarities with
C and C derivates, but obviously some important and IMHO unintuitive
differences.

At least for now it seems prudent to avoid such edge-cases and to add
parentheses around assignment operations which are part of more complex
expressions.

>> [1] <http://www.php.net/manual/en/language.operators.precedence.php>

--
Christoph M. Becker
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Correlating curl resources to some other object.
Next Topic: Experienced Web designer required
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri May 10 00:01:32 GMT 2024

Total time taken to generate the page: 0.05176 seconds