PHP 8.2: ${var} string interpolation deprecated

Version8.2
TypeDeprecation

PHP supports replacing variable values within a string literal with double quotes and heredoc syntax:

$name = 'PHP';
echo "Hello $name"; // Hello PHP

It is also possible to surround the variable name with curly braces for better visibility:

$name = 'PHP';
echo "Hello {$name}"; // Hello PHP

Although the two patterns above are the most prominent, there are two other patterns that PHP supports, but are deprecated since PHP 8.2:

Dollar sign ($) outside of the curly braces

Since PHP 8.2, PHP emits a deprecation notice on the following pattern that the dollar sign ($) is placed at the outside of the curly braces:

echo "Hello ${name}";
Deprecated: Using ${var} in strings is deprecated, use {$var} instead in ... on line ...

To avoid the deprecation notice, replace the variable to have the curly braces cover the dollar sign. This pattern is compatible across PHP versions, and does not cause the deprecation notice.

- echo "Hello ${name}";
+ echo "Hello {$name}";

Variable Variables or Expressions

Variable variables are when the name of the variable is either a return value of an expression, or the value of another variable. PHP has limited support for variable variables in string literals.

Since PHP 8.2, placing the dollar sign outside the curly brace is deprecated when the expression inside the braces resolves to a variable or an expression.

The following snippet uses a variable-variable inside a string literal:

$name = 'PHP';
$var = 'name';

echo "Hello $$var"; // Hello PHP
echo "Hello {$$var}"; // Hello PHP

Both of the instances above are not deprecated, but placing the dollar sign outside the curly brace is deprecated in PHP 8.2 and later. For example, the following snippet emits a deprecation notice:

$name = 'PHP';
$var = 'name';

echo "Hello ${$var}"; // Hello PHP
Deprecated: Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead in ... on line ...

To avoid the deprecation notice, move the dollar sign inside the curly braces:

- echo "Hello ${$var}"; // Hello PHP
+ echo "Hello {$$var}"; // Hello PHP 

PHP also supports using an expression inside a string literal to resolve to a variable name:

echo "Hello $$object->getMethod()"

Similar to the example of variable-variable that emits a deprecation notice, placing a curly brace with the dollar sign outside emits a deprecation notice:

Deprecated: Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead in ... on line ...

Placing an additional curly brace around the expression avoids the deprecation notice:

- echo "Hello ${$object->getMethod()}"
+ echo "Hello {${$object->getMethod()}}"

Backwards Compatibility Impact

PHP 8.2 deprecates patterns with variables in string literals to use curly braces in a way that the dollar sign is placed inside the braces. Additionally, expressions that resolve to a variable name must place curly braces around the expression itself.

The replacement examples mentioned above are compatible with all PHP versions.

In PHP 9.0, it is planned to throw an exception on the deprecated patterns.


RFC Discussion Implementation