Home > Article > Backend Development > Detailed explanation of $$ in php
This article introduces to you the explanation of "$$" in the variable variable $$str in PHP. It is very simple and practical, and comes with simple examples. I hope to be helpful.
This way of writing is called a variable variable
Sometimes it is very convenient to use variable variable names. That is, the variable name of a variable can be set and used dynamically. An ordinary variable is set through declaration, for example:
<?php $a = "hello"; ?>
A variable variable obtains the value of an ordinary variable as the variable name of the variable variable. In the above example, hello can be used as a variable variable after using two dollar signs ($). For example:
<?php $$a = "world"; ?>
At this time, two variables are defined: the content of $a is "hello" and the content of $hello is "world". Therefore, it can be expressed as:
<?php echo "$a ${$a}"; ?>
The following writing is more accurate and will output the same result:
<?php echo "$a $hello"; ?>
They will both output: hello world.
To use mutable variables with arrays, an ambiguity must be resolved. This is when writing $$a[1], the parser needs to know whether it wants $a[1] as a variable, or whether it wants $$a as a variable and extracts the variable with index [1] value. The syntax to solve this problem is to use ${$a[1]} for the first case and ${$a}[1] for the second case.
Related recommendations:
Beginners to learn PHP variables, constants, arrays, functions
A brief discussion on the characters available for PHP variables_PHP tutorial
The above is the detailed content of Detailed explanation of $$ in php. For more information, please follow other related articles on the PHP Chinese website!