php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #28760 Precedence of + - . incorrect in docs.
Submitted: 2004-06-13 01:37 UTC Modified: 2004-07-07 10:35 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: james at gogo dot co dot nz Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.3.4 OS: Linux
Private report: No CVE-ID: None
 [2004-06-13 01:37 UTC] james at gogo dot co dot nz
Description:
------------
In http://www.php.net/manual/en/language.operators.php the relative precedence of + - and . is listed as '+ - .'  indicating that + and - operations should happen before . operations.  

However the code here shows that concatenation happens first.

Personally I would prefer that concatenation happened last as in the docs as it would be much more sensible (you hardly ever want to use + - on a result of a concatenation, but often want to use the result of a + - in a concatenation), but that's obviously too big a change now.

Reproduce code:
---------------
echo 'Foo' . 5 - 5;
echo "\n";
echo 'Foo' . 5 + 5;

Expected result:
----------------
I expect to get the following
---
Foo0
Foo10
---
(+ and - happen first, then concatenation, as according to documentation)

Actual result:
--------------
The result you get is
---
0
5
---
(. happens first, then the resulting string is evaluated as int producing 0 which is combined with the next operator and value to produce the result).


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-06-13 04:52 UTC] curt@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

the output should really be:
-5
5

if operator precedence is equal, left to right associativity is used. To obtain preferred precendence use ().

 [2004-06-13 14:59 UTC] derick@php.net
This is not a documentation problem at all, please don't close it as that.
 [2004-07-05 10:40 UTC] mgf@php.net
Actually, taking another look at this, although I think you'd have to be unbelievably literal-minded to take the wrong interpretation that's been taken here, the manual is, perhaps, a little less exact than it could be.

The sentence at the top of the operator-precedence table that reads:

"The following table lists the precedence of operators with the highest-precedence operators listed first."

could just be interpreted so that the single entry for "+ - ." is taken to mean that "+" is of higher precedence than "-" is of higher precedence than ".".  The problem word here is "before" -- perhaps it should read "above" (or something similar).  Also, there's no reference here to what the "Associativity" column might be about.  So, all in all, perhaps this sentence should be expanded slightly to something like:

"The following table lists the precedence of operators with the highest-precedence operators listed at the top of the table.  Operators on the same line have equal precedence, in which case their associativity decides which order to evaluate them in."

Cheers!

Mike
 [2004-07-07 10:35 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 03 13:01:32 2024 UTC