Home >Backend Development >PHP Tutorial >Detailed explanation of PHP variable variable examples

Detailed explanation of PHP variable variable examples

怪我咯
怪我咯Original
2017-04-27 17:13:224835browse

What is a variable variable?

There is another type of variable in PHP, the "variable variable". A mutable variable is a unique variable in PHP that allows the name of a variable to be changed dynamically.

How mutable variables work

This feature works by using the value of one variable as the name of another variable. For example, we can set the value of $str:
$name = 'str';
So, we can replace $str with

name.

name = 5;
This code is equivalent to:
$str= 5;

Usage method

Use variable A variable means adding an extra dollar sign "$" in front of the variable.

Using mutable variables for arrays

In order to use mutable variable names for arrays, 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 solve this ambiguity problem is: use ${$a[1]} in the first case, use ${$a}[1] in the second case

Class attributes can also be passed through variable attribute names Come visit. 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 used as a property name of the $foo object. It can also be used if $bar is an array.

Detailed explanation of PHP variable variable examplesVariable variable names cannot be used for super global array variables in PHP functions and classes. The variable $this is also a special variable that cannot be dynamically named.

Example

The following example uses a mutable variable to change the name of the variable. The code is as follows:

<?php
header("content-type:text/html;charset=utf-8");                //设置编码
$str=&#39;trans&#39;;                                                  //声明变量$str
$trans=&#39;PHP中文网&#39;;                                            //声明变量$trans

echo $str;                                                     //输出变量$str

echo "<br/>";

echo $$str;                                                 //通过可变变量输出$trans的值

?>

Detailed explanation of the example:

First define two variables $str and $trans, and output the value of variable $str, and then use a variable variable to change the name of variable $str , and finally output the variable value after changing the name

The running results are as follows:

Detailed explanation of PHP variable variable examples

The above is a simple usage of variable variables, the next section , introduce to you "PHP's predefined variables"

Related video tutorial recommendations: "php.cn Dugu Jiujian (4)-php video tutorial" Variable variables: Provides a new idea for creating new variables

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

Statement:
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