Detailed explanation of how to use variable variable names in PHP

怪我咯
Release: 2023-03-10 20:56:02
Original
1245 people have browsed it

Sometimes variable variable names will bring great convenience to programming. That is to say, variable names can be named and used dynamically. Usually variables are named by the following statement:

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

Variable variableThe name refers to using the value of a variable as the name of the variable. In the example above, you can set hello to the name of a variable by using two $ signs, like below.

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

Through the above two statements, two variables are defined: variable $a, which contains "hello" and variable $hello, which contains "world". Therefore, the output of the following language:

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

is exactly the same as the output of the following statement:

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

They both output: hello world.

In order to use the mutable variable name of an array, you need to resolve an ambiguity problem. That is, if you write $$a[1], the parser needs to understand whether you mean to treat $a[1] as a variable, or to treat $$a as a variable. [1] refers to this variable. index. The syntax to resolve this ambiguity is: use ${$a[1]} in the first case and ${$a}[1] in the second case.

ClassProperties can also be accessed through mutable property names. Variable property names are taken from the access scope of the variable in which the call was made. For example, if your expression is like this: $foo->$bar, then the runtime will look for the variable $bar in the local variable scope, and its value will be Will be used as a property name of the $foo object. It can also be used if $bar is an array.

Example 1 Variable variable name

<?php 
    class foo { 
        var $bar = &#39;I am bar.&#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 . "n"; 
    echo $foo->$baz[1] . "n"; 
    ?>
Copy after login

The above example will output the following results:

I am bar.
I am bar.
Copy after login

Warning

Please note that variable variable name It cannot be used for PHP functions and super global array variables in classes. The variable $this is also a special variable that cannot be dynamically named.

The above is the detailed content of Detailed explanation of how to use variable variable names in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!