rfc:concatenation_precedence

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:concatenation_precedence [2019/03/28 09:57] – created bwoebirfc:concatenation_precedence [2020/08/01 23:54] (current) – RFC was implemented carusogabriel
Line 1: Line 1:
 ====== PHP RFC: Change the precedence of the concatenation operator ====== ====== PHP RFC: Change the precedence of the concatenation operator ======
-  * Version: 0.9+  * Version: 1.0
   * Date: 2019-03-28   * Date: 2019-03-28
   * Author: Bob Weinand, bobwei9@hotmail.com   * Author: Bob Weinand, bobwei9@hotmail.com
-  * Status: Draft+  * Status: Implemented
   * First Published at: http://wiki.php.net/rfc/concatenation_precedence   * First Published at: http://wiki.php.net/rfc/concatenation_precedence
  
 ===== Introduction ===== ===== Introduction =====
  
-It's been a long standing issue that an expression like <php>echo "sum: " . $a + $b;</php> evaluates from left to right as <php>echo ("sum: " . $a) + $b;</php> instead of an intuitively expected and desired behavior of the addition having a higher precedence: <php>echo "sum :" . ($a + $b);</php>.+It's been a long standing issue that an (unparenthesized) expression with '+', '-' and '.' evaluates left-to-right. 
 + 
 +<code php> 
 +echo "sum: " . $a + $b; 
 + 
 +/current behavior: evaluated left-to-right 
 +echo ("sum: " . $a) + $b; 
 + 
 +// desired behavioraddition and subtraction have a higher precendence 
 +echo "sum :" . ($a + $b); 
 +</code>
  
 This RFC aims to change that behavior to be less error-prone and more intuitive. This RFC aims to change that behavior to be less error-prone and more intuitive.
Line 18: Line 28:
 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. 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.+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 ===== ===== Backward Incompatible Changes =====
  
-Every unparenthesized expression featuring an '-' or '+' after a '.' will change behavior. As an example, the expression <php>"3" . "5" + 7</php> will now be equal to 312 instead of previously 42.+Every unparenthesized expression featuring an '-' or '+' after a '.' will change behavior. As an example, the expression <php>"3" . "5" + 7</php> will now be equal to "312instead 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. 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) ===== ===== Proposed PHP Version(s) =====
  
-The next 7.x (currently PHP 7.4).+PHP 8, with a deprecation notice in PHP 7.4 upon encountering an unparenthesized expression containing an '.' before a '+' or '-'
  
-Or, alternatively as a gentler upgrade, we may choose to fail hard in PHP 7.4, i.e. failing the compilation if an unparenthesized expression containing an '.' before a '+' or '-'. And only do the actual proposed change in PHP 8.+===== Vote =====
  
-===== Proposed Voting Choices =====+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.
  
-"Change behavior in PHP 7.4""Fail in PHP 7.4 and change in PHP 8""Neither".+<doodle title="Change the precedence of the concatenation operator as proposed in PHP 8?auth="bwoebi" voteType="singleclosed="true"
 +   * Yes 
 +   * No 
 +</doodle>
  
-The RFC requires 2/3 majority for "Change behavior in PHP 7.4". For the "Fail in PHP 7.4 and change in PHP 8option to pass a combined 2/3 majority of "Change behavior in PHP 7.4and "Fail in PHP 7.4 and change in PHP 8is required.+<doodle title="Emit deprecation notice in PHP 7.4 if the RFC is accepted?auth="bwoebivoteType="singleclosed="true"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
 ===== Patch ===== ===== Patch =====
  
-The patch for the change itself is trivial, requiring small changes in the zend_ast.c and zend_language_parser.y to change the precendences. I'll add it later.+  * https://github.com/php/php-src/pull/4001 (PHP 7.4) 
 +  * https://github.com/php/php-src/pull/4002 (PHP 8)
rfc/concatenation_precedence.1553767075.txt.gz · Last modified: 2019/03/28 09:57 by bwoebi