Home >Backend Development >PHP Tutorial >Detailed explanation of '$$' in $$str in php
This way of writing is called a variable variable Sometimes it is convenient to use mutable variable names. That is, the variable name of a variable can be set and used dynamically. An ordinary variable is set by declaration, for example:
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:
At this point, both variables are defined: the content of $a is "hello" and the content of $hello is "world". Therefore, it can be expressed as:
The following way of writing is more accurate and will output the same result:
They will all 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. The above is the entire content of this article, I hope you all like it. |