Change the precedence of the concatenation operator View as Markdown

Implemented (PHP 7.4)
Authors
Bob Weinand, bobwei9@hotmail.com
Date
2019-03-28
Version
1.0
Source
wiki.php.net/rfc/concatenation_precedence

Votes

Change the precedence of the concatenation operator as proposed in PHP 8? closed
Yes
31
No
4
35 voters
andrey No 2019-05-07 12:46
ashnazg Yes 2019-05-01 14:05
bishop Yes 2019-05-01 01:37
bwoebi Yes 2019-05-01 09:50
carusogabriel Yes 2019-05-02 07:36
cpriest Yes 2019-05-15 23:18
daverandom Yes 2019-05-07 11:42
derick Yes 2019-05-01 08:33
diegopires Yes 2019-05-14 14:06
duncan3dc Yes 2019-05-02 17:07
emir Yes 2019-05-12 08:14
galvao Yes 2019-05-01 11:46
gasolwu Yes 2019-05-04 14:25
jasny Yes 2019-05-05 10:20
kalle Yes 2019-05-07 09:40
kelunik Yes 2019-05-03 14:19
kguest Yes 2019-05-02 07:16
kinncj Yes 2019-05-01 13:48
laruence No 2019-05-07 03:56
levim Yes 2019-05-09 14:43
malukenho Yes 2019-05-01 10:05
marcio Yes 2019-05-12 00:00
mcmic Yes 2019-05-02 09:40
ocramius Yes 2019-05-01 17:50
patrickallaert Yes 2019-05-02 22:03
peehaa Yes 2019-05-03 13:33
petk Yes 2019-05-03 17:49
pmmaga Yes 2019-05-06 10:17
pollita Yes 2019-05-02 13:53
salathe No 2019-05-01 08:50
sammyk Yes 2019-05-11 22:10
santiagolizardo Yes 2019-05-13 21:03
sergey Yes 2019-05-01 04:33
stas No 2019-05-03 18:35
trowski Yes 2019-05-03 14:22
Emit a deprecation notice in PHP 7.4 if the RFC is accepted? closed
Yes
30
No
7
37 voters
andrey No 2019-05-07 12:46
ashnazg Yes 2019-05-01 14:05
bishop Yes 2019-05-01 01:37
bwoebi Yes 2019-05-01 09:50
carusogabriel No 2019-05-02 07:36
cpriest Yes 2019-05-15 23:18
daverandom Yes 2019-05-07 11:42
derick Yes 2019-05-01 08:33
diegopires No 2019-05-14 14:06
duncan3dc Yes 2019-05-02 17:07
emir Yes 2019-05-12 08:14
galvao Yes 2019-05-01 11:46
gasolwu Yes 2019-05-04 14:25
girgias Yes 2019-04-30 22:44
jasny Yes 2019-05-05 10:20
kalle Yes 2019-05-07 09:40
kelunik Yes 2019-05-03 14:19
kguest Yes 2019-05-02 07:16
kinncj No 2019-05-01 13:49
levim Yes 2019-05-03 16:57
malukenho Yes 2019-05-01 10:05
marcio Yes 2019-05-12 00:00
mcmic Yes 2019-05-02 09:41
ocramius Yes 2019-05-01 17:50
patrickallaert Yes 2019-05-02 22:04
peehaa Yes 2019-05-03 13:33
petk Yes 2019-05-03 17:49
pmmaga Yes 2019-05-06 10:17
pollita Yes 2019-05-02 13:53
rasmus No 2019-05-08 20:47
salathe No 2019-05-16 15:57
sammyk Yes 2019-05-11 22:10
santiagolizardo Yes 2019-05-13 21:03
sergey Yes 2019-05-07 13:59
stas No 2019-04-30 22:45
trowski Yes 2019-05-03 14:22
yunosh Yes 2019-05-02 08:18

Discussions on the mailing list

[RFC] Change the precedence of the concatenation operator
by Bob Weinand · 18 messages
[RFC] [Vote] Change the precedence of the concatenation operator
by Bob Weinand · 4 messages
Ternary associativity
by Nikita Popov · 8 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

It's been a long standing issue that an (unparenthesized) expression with '+', '-' and '.' evaluates left-to-right.

echo "sum: " . $a + $b;
 
// current behavior: evaluated left-to-right
echo ("sum: " . $a) + $b;
 
// desired behavior: addition and subtraction have a higher precendence
echo "sum :" . ($a + $b);

This RFC aims to change that behavior to be less error-prone and more intuitive.

Proposal

Currently the precedence of '.', '+' and '-' operators are equal. Any combination of these operators are simply evaluated left-to-right.

This is counter-intuitive though: you rarely want to add or subtract concatenated strings which in general are not numbers. However, given PHPs capability of seamlessly converting an integer to a string, concatenation of these values is desired.

Thus, the RFC proposes to give '.' an inferior precedence to '+' and '-', so that additions and subtractions are always performed before the concatenation. Concretely, the new precedence will be right below '<<' and '>>' operators, given them also being “math” operators and not usable with non-numeric strings.

Backward Incompatible Changes

Every unparenthesized expression featuring an '-' or '+' after a '.' will change behavior. As an example, the expression "3" . "5" + 7 will now be equal to “312” instead of previously 42.

While this is a subtle behavior change in that it will give different outputs without notice or warning, it is trivially possible to statically analyze the code and find all instances where this happens. As to my knowledge these occurrences are quite rare as it almost always is an error in the current form, rendering the impact minimal.

As Nikita mentioned on the mailing list: http://news.php.net/php.internals/105442 - the impact to existing open-source code is de facto invisible; all found occurrences are actual bugs. This is a strong indicator that the overall impact will also be very minimal.

Proposed PHP Version(s)

PHP 8, with a deprecation notice in PHP 7.4 upon encountering an unparenthesized expression containing an '.' before a '+' or '-'.

Vote

Voting started 2019-04-30 and ends 2019-05-14. The first (primary) voting requires a 2/3 majority. The second (secondary) voting requires a 50%+1 majority.

Vote: Change the precedence of the concatenation operator as proposed in PHP 8? — Yes: 31 · No: 4 (closed)

Vote: Emit a deprecation notice in PHP 7.4 if the RFC is accepted? — Yes: 30 · No: 7 (closed)

Patch