PHP variable Variable instance details

Source: Internet
Author: User
What is a mutable variable?

There is one other type of variable in PHP, "mutable variable." A mutable variable is a unique variant of PHP that allows you to dynamically change the name of a variable.

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 use

Name replaces $str.

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

How to use

Using mutable variables is to add a dollar sign "$" to the front of the variable.

Arrays using mutable variables

In order to use the variable variable name of the array, you need to solve a ambiguity problem. That is, if you write $ $a [1], the parser needs to know whether you mean to use $a[1] as a variable, or to use the $ $a as a variable, [1] to refer to the index of the variable. The syntax for solving this ambiguity is: The first case uses ${$a [1]}, and the second case uses ${$a}[1]

Class properties can also be accessed through variable property names. A mutable property name is obtained from the access scope of the variable where the call is generated. For example, if your expression is this: $foo-$bar, the runtime will look for the variable $bar within the scope of the local variable, and its value will be a property name for the $foo object. If the $bar is an array, it can be used.

mutable variable names cannot be used on PHP functions and super Global array variables in classes. Variable $this is also a special variable that cannot be named dynamically.

Instance

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

<?phpheader ("Content-type:text/html;charset=utf-8");                Set the encoding $str= ' trans ';                                                  Declaring variable $str$trans= ' topic.alibabacloud.com ';                                            declaring variable $transecho $str;                                                     Output variable $strecho "<br/>"; echo $ $str;                                                 Output the value of $trans through a mutable variable?>

Detailed Examples:

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

The results of the operation are as follows:

The above is a simple use of mutable variables, the next section, for you to introduce "pre-defined PHP variables"

Related video tutorial recommended: "Php.cn lonely Nine Cheap (4)-php video tutorial" mutable variable: Provides a new way to create new variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.