Home > Backend Development > PHP Tutorial > Variable variables in php (detailed code explanation)

Variable variables in php (detailed code explanation)

烟雨青岚
Release: 2023-04-08 21:26:01
forward
18467 people have browsed it

Variable variables in php (detailed code explanation)

Variable variables in php

Sometimes it is convenient to use variable variable names. That is to say, The variable name of a variable can be dynamically set and used. An ordinary variable is set through declaration, for example:

<?php
$a = &#39;hello&#39;;
?>
Copy after login

A variable variable obtains the value of an ordinary variable as the variable name of the variable variable. In the above example hello uses two dollar signs ($), it can be used as a variable variable.

Example 1:

<?php
$$a = &#39;world&#39;;
?>
Copy after login

At this time, both variables are defined: the content of $a is "hello" and the content of $hello is "world".

Example 2:

<?php
echo "$a ${$a}";
?>
Copy after login

The following statement outputs exactly the same result:

<?php
echo "$a $hello";
?>
Copy after login

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 takes out the variable The value with index [1].

The syntax to solve this problem is to use ${$a[1]} for the first case and ${$a}[1] for the second case.

Class properties can also be accessed through variable property names. Mutable property names will be resolved within the scope of the call. For example, for the expression $foo->$bar, $bar will be parsed in the local scope and its value will be used as the property name of $foo.

The same is true for $bar when it is an array unit.

You can also use curly braces to clearly delimit attribute names. Most useful when the property is in an array, or the property name contains multiple parts or the property name contains illegal characters (such as from json_decode() or SimpleXML).

Example:

<?php
class foo {
    var $bar = &#39;I am bar.&#39;;
    var $arr = array(&#39;I am A.&#39;, &#39;I am B.&#39;, &#39;I am C.&#39;);
    var $r   = &#39;I am r.&#39;;
}
$foo = new foo();
$bar = &#39;bar&#39;;
$baz = array(&#39;foo&#39;, &#39;bar&#39;, &#39;baz&#39;, &#39;quux&#39;);
echo $foo->$bar . "
";
echo $foo->$baz[1] . "
";
$start = &#39;b&#39;;
$end   = &#39;ar&#39;;
echo $foo->{$start . $end} . "
";
$arr = &#39;arr&#39;;
echo $foo->$arr[1] . "
";
echo $foo->{$arr}[1] . "
";
?>
Copy after login
The above routine will

output

:

I am bar.
I am bar.
I am bar.
I am r.
I am B.
Copy after login
Thank you for reading, I hope you have learned it mutable variable.

This article is reproduced from: https://www.cnblogs.com/ryanzheng/p/9133381.htmlRecommended tutorial: "

php tutorial

The above is the detailed content of Variable variables in php (detailed code explanation). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:51dev.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template