This notation is called a mutable variable. Sometimes it is 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 by a declaration, for example:
- $a = "Hello";
- ?>
Copy CodeA mutable variable gets the value of an ordinary variable as the variable name of the variable variable. In the example above, hello uses two dollar sign ($) and can be used as a variable variable. For example:
- $ $a = "world";
- ?>
Copy CodeAt this point, two variables are defined: $a content is "hello" and $hello content is "world". Therefore, it can be expressed as:
- echo "$a ${$a}";
- ?>
Copy CodeThe following wording is more accurate and will output the same result:
- echo "$a $hello";
- ?>
Copy CodeThey all output: Hello world. To use mutable variables with arrays, you must address an ambiguous question. This is when you write the $ $a [1], the parser needs to know if you want to $a [1] as a variable, or you want $ $a as a variable and take out the value of the variable indexed to [1]. The syntax for solving this problem is to use ${$a [1] for the first case, and ${$a}[1 for the second case). The above mentioned is the whole content of this article, I hope you can like. |