rfc:arbitrary_expression_interpolation

This is an old revision of the document!


PHP RFC: Arbitrary Expression Interpolation

Introduction

Interpolation is a widely used feature in PHP. It is, however, constrained to variable-like expressions only, which makes its usage somewhat limited. I would therefore like to propose for the ability to interpolate arbitrary expressions in PHP.

Proposal

This proposal introduces a new interpolation syntax: #{}. Any expression within the parentheses will be evaluated and stringified, concatenating it to the rest of the string. This change affects double-quoted strings, heredocs, and the execution operator (shell execution via backticks).

This will:

  • Give greater flexibility to developers when constructing strings from any expressions
  • Make heredocs a more useful feature, since they will not be constrained to evaluating variable-like expressions only

Some examples:

$value = 10;
var_dump("Result: #{$value * 5}"); // string(10) "Result: 50"
 
function a()
{
    return "def";
}
 
var_dump("abc #{a()}"); // string(7) "abc def"
 
`echo #{$value * 5}`, // 50

echo <<<END
Result: #{$value * 5}
END; // "Result: 50"

Backward Incompatible Changes

The new syntax will now cause the character sequence #{...} to be evaluated within strings.

Proposed PHP Version(s)

The next PHP 7.x version (or 8.0, whichever comes next)

RFC Impact

To Opcache

Still need to be checked.

Proposed Voting Choices

A simple yes or no for this feature (with a 2/3 +1 majority required).

Patches and Tests

Initial implementation: https://github.com/php/php-src/compare/master...tpunt:arbitrary-expression-interpolation

Language specification: will be updated if the RFC is accepted.

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged to
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature
  4. a link to the language specification section (if any)

References

Links to external references, discussions or RFCs

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/arbitrary_expression_interpolation.1505127080.txt.gz · Last modified: 2017/09/22 13:28 (external edit)