PHP learning-Variable variables,-Variable variables. PHP learning-Variable variables and variable variables-sometimes it is convenient to use variable names. That is to say, the variable name of a variable can be dynamically set and used. Learning from a common PHP -- Variable variables and -- Variable variables
Variable
Sometimes it is convenient to use a variable name. That is to say, the variable name of a variable can be dynamically set and used. A common variable is set through declaration, for example:
A variable obtains the value of a common variable as the variable name of the variable. In the preceding exampleHelloAfter two dollar signs ($) are used, the variable can be used as a variable. For example:
At this time, both variables are defined:$The content is "hello" and$ HelloThe content is "world ". Therefore, the following statement:
The output result is exactly the same as that of the following statement:
They all output: hello world.
To use variables in an array, you must solve an ambiguous problem. This is when$ A [1]The parser needs to know$ A [1]As a variable, I still want$Act as a variable and retrieve the value of [1] in the variable. The syntax for solving this problem is:$ {$ A [1]}For the second scenario$ {$ A} [1].
Class attributes can also be accessed through variable attribute names. Variable attributes will be parsed within the scope of the call. For example$ Foo-> $ barThe expression will be parsed in the local range.$ BarAnd its value will be used$ Foo. For$ BarThe same is true when it is an array unit.
You can also use curly brackets to clearly define attribute names. It is most useful when an attribute is located in an array, or when the attribute name package contains multiple parts or the attribute name package contains invalid characters (for example, from json_decode () or SimpleXML ).
Example #1 variable attributes
$bar . "\n";echo $foo->$baz[1] . "\n";$start = 'b';$end = 'ar';echo $foo->{$start . $end} . "\n";$arr = 'arr';echo $foo->$arr[1] . "\n";echo $foo->{$arr}[1] . "\n";?>
The above routine will output:
I am bar.I am bar.I am bar.I am r.I am B.
Example #2 examples of variable attributes
Example #3 variable attributes
The above routine will output:
JohnDoePHP.netJohnDoePHP.net
From: http://php.net/manual/zh/functions.variable-functions.php
Variable names are sometimes convenient to use. That is to say, the variable name of a variable can be dynamically set and used. A general...