PHP mutable variables

Source: Internet
Author: User

Preface: This blog mainly explains the function and usage of PHP variable variables. The examples in this article come from http://php.net/manual/zh/language.variables.variable.php, which are slightly altered.

Body:

There is a variable in PHP called a mutable variable, which is not a variable of the underlying type. A mutable variable refers to the value of a common variable that can be used as the name of another variable. It sounds a bit abstract. We can demonstrate the definition and utility of variable variables through examples.

1 $a = ' Hello '; 2 $$a = ' world '; 3 Echo $hello;

In the above code, the first line is a normal variable definition, the variable name is a, and the value of the variable is hello. The second line uses the value of variable A, which defines a variable whose name is Hello (the value of a) and the value is world. The result of outputting this variable is: world.

Let's look at a slightly more complicated example

1 $b=Array("I", "AM", "a", "student"));2 $I= "a I";3 $am= "Word AM";4 $a= "an A";5 $student= "a STUDENT";6 $var= ' B ';7 Echo${$b[1]};8 Echo${$var}[1];

The 1-6 lines of this code above are defined as variables. The result of line 7th is: Word AM. Here we explain the results of line 7th. The value of $b [1] is the second element in this array, that is am, then ${$b [1]} is ${am}, i.e. $am (curly braces can be removed). The result of line 7th, then, is the value of the variable $am. The result of line 8th is: AM. Here we explain the results of line 8th. First, the server resolves the value of $var, $var = ' B ', so ${$var}[1] is ${b}[1], which is the value of $b[1], which is am.

The above two examples illustrate the use of mutable variables in a more simple way. In fact, mutable variables can also be used in object-oriented PHP. In the following example, we explain the application of variable variables in object-oriented.

1 classfoo{2     var $bar= ' I am Bar ';3     var $arr=Array(' I am A ', ' I am B ', ' I am C ');4     var $r= ' I am R ';5 }6 7 $bar= ' Bar ';8 $baz=Array(' foo ', ' Bar ', ' baz ', ' Quux '));9 Ten $myfoo=Newfoo (); One Echo $myfoo-$bar." <br> "; A Echo $myfoo-$baz[1]. " <br> "; -  - $start= ' B '; the $end= ' AR '; - Echo $myfoo->{$start.$end}." <br> "; -  - $arr= ' arr '; + Echo $myfoo-$arr[1]. " <br> "; - Echo $myfoo->{$arr}[1]. " <br> ";

The above code defines a class: Foo.

The result of line 11th is: I am bar. Because in the process of parsing the $myfoo-> $bar, the interpreter will first parse $bar, $bar the value is bar, then $myfoo-> $bar equivalent to $myfoo call the value of the property $bar in its class, the result is: I am bar.

The result of Line 12th is: I am bar. Because in the process of parsing $myfoo-> $baz [1], the interpreter first parses $baz[1], $baz [1] is bar, then $myfoo-> $baz [1] equals $myfoo Call the value of the property $bar in its class, the result is: I am Bar

The result of line 16th is: I am bar. Because in parsing $myfoo->{$start, $end}, first parse the values of $start and $end, respectively "B" and "AR", through the connector "." After the connection result is bar, then $myfoo->{$start, $end} result is $myfoo call the value of the property $bar in its class, the result is: I am bar.

The result of line 19th is: I am R. Because in the process of parsing $myfoo-> $arr [1], the value of $arr[1] is first parsed, which is R. The value of $arr [1], $myfoo, is the value that calls the property r in its class, and the result is: I am R.

The result of Line 20th is: I am B. Because in parsing $myfoo->{$arr}[1], the value of the $arr is parsed first, for arr, then the value of {$arr}[1] is also the value of the second property that calls the $myfoo property $arr, the result is: I am B.

PHP mutable 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.