Home >Backend Development >PHP Tutorial >How Does PHP's `$$` (Double Dollar) Notation Work with Variable Variables?

How Does PHP's `$$` (Double Dollar) Notation Work with Variable Variables?

DDD
DDDOriginal
2024-12-06 03:28:09643browse

How Does PHP's `$$` (Double Dollar) Notation Work with Variable Variables?

PHP's Variable Variables: Decoding $$ (Double Dollar)

When encountering the $$ notation within PHP, it's essential to delve into the concept of variable variables. This syntax, denoted as $$variable, allows you to access and manipulate variables whose names are stored in other variables.

Imagine the following code snippet within a function:

global $$link;

Here, $$link represents a variable variable, where $$ indicates that the variable name itself is stored in the $link variable. Consider the following example:

$real_variable = 'test';
$name = 'real_variable';
echo $$name; // Outputs: 'test'

In this case:

  • $real_variable stores the value 'test'.
  • $name contains the name of $real_variable ('real_variable').
  • $$name translates to "the variable whose name is stored in $name," which is $real_variable.
  • Therefore, $$name effectively retrieves the value of $real_variable, which is 'test'.

Note that this mechanism can extend beyond "double variables." For example:

$real_variable = 'test';
$name = 'real_variable';
$name_of_name = 'name';

echo $$name_of_name; // Outputs: 'real_variable'
echo $$$name_of_name; // Outputs: 'test'

Here, $$$name_of_name navigates one level deeper, accessing the variable whose name is stored in the variable named by $name_of_name.

The above is the detailed content of How Does PHP's `$$` (Double Dollar) Notation Work with Variable Variables?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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