php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #17180 Operator Precedence
Submitted: 2002-05-13 09:36 UTC Modified: 2003-01-18 16:14 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:2 (66.7%)
From: sitnikov at infonet dot ee Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.2.0 OS:
Private report: No CVE-ID: None
 [2002-05-13 09:36 UTC] sitnikov at infonet dot ee
Why & How this code will work?

  <?
   function foo($flag)
   {
      return $flag;
   }

   $a=TRUE;

   echo "if (!\$a = foo(FALSE))) is ";

   if (!$a = foo(FALSE))
     echo "true";
   else
     echo "false";

   echo "\n";
   var_dump($a);
   echo "\n";
  ?>
  
  Output:

  if (!$a = foo(FALSE))) is true
  bool(false)

  http://www.php.net/manual/en/language.operators.php "Operator Precedence"

  `!` has more precedence than `=`

  And after `!` we must have boolean constant in left side:

  FALSE = foo()

  Explain to me pls that I do not understand

  P.S. in C & Perl (!$a = foo()) is not valid expression

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-05-13 17:48 UTC] manuzhai@php.net
"if (!$a = foo(FALSE))" --> you're assigning the output of foo(FALSE) to $a
"if (!$a == foo(FALSE))" --> you're comparing !$a and foo(FALSE)
 [2002-05-13 17:56 UTC] sitnikov at infonet dot ee
Yes, I want ASSIGN value to $a and check assigned value.

But parser must say: "parser error", becouse it can not assign value to constant.


Please reopen.
 [2002-05-13 18:20 UTC] manuzhai@php.net
Well, but it's stupid to do something like that. It makes no sense to assign anything to NOT(a variable), so PHP takes care of that by changing the precedence a little in this case.
 [2002-05-13 18:30 UTC] sitnikov at infonet dot ee
This behaviour is capable to confuse the developer and if this is "features" it must be documented in manual.
 [2002-05-14 13:46 UTC] phpclub at digiways dot com
Actually this is a bug, since in PHP manual it's clearly stated that ! operator has a priority over = operator.

> It makes no sense to assign anything to NOT(a variable),
> so PHP takes care of that by
> changing the precedence a little in this case.

In other words - if user makes a mistake and writes illegal code, PHP takes care about that and makes this code work (but in a way different from what developer has expected).

Also if you consider any other programming languages,
if you write a code which should not compile by language specifications (like the above code in PHP), no compiler will try to "take care" of that.

If you insist on that "care", then you definetely have to reflect that in the manual, otherwise it's nothing but a bug.
 [2002-05-16 13:27 UTC] derick@php.net
Marking this as a doc problem.
 [2003-01-18 16:14 UTC] philip@php.net
This has now been documented:
http://cvs.php.net/cvs.php/phpdoc/en/language/operators.xml

Thanks for the report :)
 [2003-01-20 12:06 UTC] m dot ford at lmu dot ac dot uk
There seems to be a more general principle at work here -- see, for example, related bug report 17386 (http://bugs.php.net/bug.php?id=17386).

Would it be true to say something like: when an assignment operator occurs in a position where the usual rules of precedence would produce a parse error, the precedence of the assignment is artificially increased to allow the expression to parse; this usually gives the intended result.

Cheers!

Mike
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Aug 15 19:01:28 2024 UTC