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 #184996 is a reply to message #184990] Sun, 23 February 2014 19:42 Go to previous messageGo to previous message
GS is currently offline  GS
Messages: 3
Registered: February 2014
Karma:
Junior Member
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.

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

"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!"

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?)
[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:49:27 GMT 2024

Total time taken to generate the page: 0.05188 seconds