Sometimes variable names bring great convenience to programming. That is to say, the variable name can be dynamically named and used. Generally, variables are named using the following statement :? Php $ ahello ;? A variable name uses the value of a variable as the name of the variable. In the above example, by using two $ symbols, you can
Sometimes variable names bring great convenience to programming. That is to say, the variable name can be dynamically named and used. Generally, variables are named using the following statement :? Php $ a = hello ;? A variable name uses the value of a variable as the name of the variable. In the above example, by using two $ symbols, you can
Sometimes variable names bring great convenience to programming. That is to say, the variable name can be dynamically named and used. Generally, variables are named using the following statement:
-
- $a = 'hello';
- ?>
A variable name uses the value of a variable as the name of the variable. In the above example, by using two $ symbols, you can set hello as a variable name, as shown below.
-
- $$a = 'world';
- ?>
Using the preceding two statements, two variables are defined: the variable $ a, which contains "hello" and the variable $ hello, and the variable "world ". The following language:
-
- echo "$a ${$a}";
- ?>
The output is exactly the same as that of the following statement:
-
- echo "$a $hello";
- ?>
They all output: hello world.
To use the variable name of the array, you need to solve the ambiguity problem. That is, if you write $ a [1], the parser needs to understand whether you mean to treat $ a [1] as a variable, we still need to regard $ a as a variable. [1] refers to the index of this variable. The syntax for solving this ambiguity is: $ {$ a [1]} is used in the first case, and $ {$ a} [1] is used in the second case.
Class attributes can also be accessed through variable attribute names. The variable property name is obtained from the access range of the variable where the call is generated. For example, if your expression is like this: $ foo-> $ bar, the runtime will look for the variable $ bar in the local variable range, the value is used as an attribute name of the $ foo object. It can also be used if $ bar is an array.
Example 1 variable name
-
- class foo {
- var $bar = 'I am bar.';
- }
-
- $foo = new foo();
- $bar = 'bar';
- $baz = array('foo', 'bar', 'baz', 'quux');
- echo $foo->$bar . "\n";
- echo $foo->$baz[1] . "\n";
- ?>
The above example will output the following results:
I am bar.
I am bar.
Warning
Note that the variable name cannot be used for super Global Array variables in PHP functions and classes. Variable $ this is also a special variable that cannot be dynamically named.
Variable variables
Http://www.aqee.net/2010/11/04/variable-variables/.