Home > Backend Development > PHP8 > PHP 8.2 no longer supports using ${} to insert variables into strings

PHP 8.2 no longer supports using ${} to insert variables into strings

Release: 2023-02-17 12:40:01
forward
3789 people have browsed it

The PHP community passed a proposal with only one negative vote at the end of April. The content of the proposal is that in the upcoming PHP 8.2, the syntax of using ${} to insert variables in a string will no longer be supported (marked as deprecated) status), and that syntax was removed in PHP 9.

"PHP

Currently PHP can insert variables into strings with double quotes (") and heredoc in the following ways.

  • Insert the variable directly: “$foo”

  • Add curly braces outside the variable: “{$foo} ”

  • ##Add curly braces after the

    $ symbol: “${foo}”

  • Define variable variable syntax (

    “${expr}”, equivalent to (string) ${expr}

Ilija Tovilo, the developer who submitted the proposal, believes that the 1st and 2nd methods have their own advantages and disadvantages. The 3rd and 4th writing methods are easy to confuse, and 4 has completely different semantics (variable variables), which are rarely used. A way to insert variables into a string.

Therefore, the RFC submitted by Ilija Tovilo proposes to deprecate the above syntax 3 and 4 in PHP 8.2 and delete it in PHP 9.0.

var_dump("${foo}");
// Deprecated: Using ${} in strings is deprecated
 
var_dump("${(foo)}");
// Deprecated: Using ${} (variable variables) in strings is deprecated
Copy after login

Regarding the impact of this proposal, Ilija Tovilo analyzed the Top 1000 repositories on Packagegist and found that the above 3 syntax was used 267 times, and 4 only 0 times.

Analysis Source code: https://gist.github.com/iluuu1994/05427dd74100af8e41ebff3d4201742c

Ilija Tovilo believes that many other programming languages ​​also use the

${foo} syntax to insert in a string Variables, such as bash and JavaScript (in Template Literals). But their behavior is different from that in PHP. In PHP, this syntax means defining mutable variables. In JavaScript, it supports arbitrary expressions In PHP's current form, options 3 and 4 above are of limited use and can be confusing to developers using other programming languages ​​with similar syntax because they behave completely differently.

In the future Ilija Tovilo hopes to support the following syntax:

var_dump("{$:func()}")
Copy after login

He believes that if one decides to do this, it will be necessary to remove the less useful options first to avoid further adding to the confusion. This is what Ilija Tovilo hopes to achieve by submitting this RFC Goal.

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of PHP 8.2 no longer supports using ${} to insert variables into strings. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:OSC开源社区
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template