====== PHP RFC: Arbitrary Expression Interpolation ====== * Version: 0.9 * Date: 2017-09-10 * Author: Thomas Punt, tpunt@php.net * Status: Draft * First Published at: https://wiki.php.net/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; function someFunc() { return "def"; } var_dump( "Result: #{$value * 5}", // string(10) "Result: 50" "abc #{someFunc()}", // string(7) "abc def" `echo #{$value * 5}` // string(3) "50\n" ); echo << ==== Syntax Choice ==== There were a few different candidates regarding the syntactic choice, including: * ''${}'' - Poses a very large BC break, since ''${a}'' would now look for a constant (rather than a variable) named ''a'' * ''{}'' - Poses a potentially large BC break by suddenly giving all curly braces in strings semantic meaning * ''#{}'' - Poses a low BC break * Sting sigils (such as: ''e"Result: {func()}"'') - Poses no BC break, but looks odd to apply to the execution operator (''e`...`''), and looks ugly for the heredoc syntax (''e<''''<''''