This is an old revision of the document!
PHP RFC: Arbitrary string interpolation
- Date: 2022-03-17
- Author: Ilija Tovilo, tovilo.ilija@gmail.com
- Status: Darf
- Target Version: PHP 8.2
- Implementation: TBD
Proposal
PHP has four types of string interpolation. The two most common forms are “$foo”
and “{$foo}”
. For details on the two other forms of string interpolation see the deprecate ${} string interpolation RFC.
All the current forms of string interpolation are limited to variables or variable chains (array access, properties, method calls, etc). This probably covers 90% of use cases for string interpolation. However, there are times when this is not enough, namely:
- Function calls
- Constants
- Class constants
- Statis method calls
$world = 'world'; echo "Hello {strtoupper($world)}!"; // Hello {strtoupper(world)} // :(
This RFC proposes to add a new form for string interpolation that works for arbitrary expressions.
$world = 'world'; echo "Hello {$:strtoupper($world)}!"; // Hello WORLD! // :) echo "{$:T_PAAMAYIM_NEKUDOTAYIM}"; echo "{$:Foo::class}"; echo "{$:self::doSomething()}";
The proposed syntax works in double quoted strings (“”
), heredoc (<<<TXT TXT) and the execution operator (``
)
Motivation
Of course, this can already be achieved in two other ways:
1. By creating a temporary local variable 2. By stopping the string and concatenating the value with ''.''.
Creating local variables is not a terrible option. It does pollute the variable symbol table a bit and it requires more visual jumping to see what value is being embedded.
Concatenation is a fair bit worse. The quotes and .
symbols lead to a symbol soup, and the approach will not work nicely with heredoc at all.
echo <<<TXT Hello TXT /* <-- Must be on a new line */ . strtoupper('world') . <<<TXT TXT;
Syntax choice
The primary advantage of the syntax “{$:expr}”
is that is it is fully backwards compatible as it leads to a syntax error in PHP 8.1 rather than being interpreted literally. It also looks familiar while being distinctly different to avoid confusion with other forms of string interpolation.
Escaping
Escaping works by adding a backslash \
after the brace {
.
echo "{\$:foo}"; // {$:foo}
If you're thinking why not before the {
, PHPs existing escaping of \{$foo}
could be considered buggy. While it escapes the {}
, it also gets printed itself.
$foo = 'bar'; echo "\{$foo}"; // \{bar}
That said, escaping the proposed string interpolation {$:
should be very rare in practice.
Backwards incompatible changes
None
Vote
Voting starts 2022-xx-x and ends 2022-xx-xx.
As this is a language change, a 2/3 majority is required.