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 #184999 is a reply to message #184996] Sun, 23 February 2014 20:23 Go to previous messageGo to previous message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma:
Senior Member
GS wrote:

> Christoph Michael Becker <cmbecker69(at)arcor(dot)de> wrote:
>
>> 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.
>>
>> Why does the mentioned statement has the "expected" result? Is it
>> reliable to write it that way?
>>
>> [1] <http://www.php.net/manual/en/language.operators.precedence.php>
>>
>
> This shows one of the major problems with PHP: there is no formal language
> definition, you have to go by examples in the help file to intuit what is
> or is not valid. Where is the BNF definition of the language? There is none
> AFAICT.

Indeed, I have not yet found a formal BNF specification in the PHP
manual, too. However, in the PHP sources there is the bison file
Zend/zend_language_parser.y, which might be regarded as syntax
specification.

> Under "Control structures" the PHP help file says "Any PHP script is built
> out of a series of statements" but offers no definition of what constitutes
> a statement. Under "Expressions" the manual says "Expressions are the most
> important building stones of PHP. In PHP, almost anything you write is an
> expression." How do we square that with the previous claim? In the end I
> think you should avoid using PHP for anything critical since it is a
> language defined by handwaving, where you can't formally determine the
> meaning of a construct, yuo either have to try it out or find an analogous
> example in the help file.
>
> In your example, the answer to why the construction works is given in a
> footnote to the section "Operator Precedence" which states

Thanks for pointing this out.

> "Note: Although = has a lower precedence than most other operators, PHP
> will still allow expressions similar to the following: if (!$a = foo()), in
> which case the return value of foo() is put into $a. "
>
> The formal meaning of this footnote is "BOOGA BOOGA!"

Um, well, yes.

From looking at the mentioned bison file the meaning of the note is
becoming clearer. Roughly summarized, expressions are not defined
"symmetrically", as for instance, in C, but somewhat "pragmatically".

The statement given above is parsed the following way (simplified):

statement ->
expr ';' ->
expr T_BOOLEAN_AND expr ';' ->
'!' variable T_BOOLEAN_AND variable '=' expr ';'

That explains why it works as described. However, I somewhat wonder why
there are two sets of boolean operators (&& and || vs. AND and OR). It
seems to me they are only different wrt. the ternary operator, but in
this case using parentheses seems to be preferable, anyway.

> This kind of thing give me the heeby jeebies and makes me think I should be
> using some other language for server side scripting (maybe perl is better
> defined?)

--
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: Sat Nov 30 23:08:13 GMT 2024

Total time taken to generate the page: 0.04899 seconds