PHP Learning-Variable variables--variable variable _php tutorial

Source: Internet
Author: User

PHP Learning-variable variables, variable variables


Variable variable

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

 
  PHP$a = ' Hello ';? >

A mutable variable gets the value of an ordinary variable as the variable name of the variable variable. In the example above, Hello uses two dollar sign ($) and can be used as a variable variable. For example:

 
  php$$a = ' world ';? >

At this point, two variables are defined: $a the content is "Hello" and $hello the content is "world". Therefore, the following statement:

 
  PHPecho "$a ${$a}";? >

Output the exact same result as the following statement:

 
  PHPecho "$a$hello";? >

They all output:Hello world.

To use mutable variables with arrays, you must address an ambiguous question. This is when the $$a[1] parser needs to know if it wants to be $a[1] a variable, or if it wants to be a variable and take $$a out the value indexed as [1] in that variable. The syntax for solving this problem is, in the first case ${$a[1]} , in the second case ${$a}[1] .

The properties of a class can also be accessed through a variable property name. The Mutable property name is resolved within the scope of the call. For example, for an $foo->$bar expression, the property name will be resolved at the local scope $bar and its value will be used $foo . $barthe same is true for array cells.

You can also use curly braces to clear the bounds of a property name. Most useful is if the property is in an array, or if the property name contains more than one part or if the property name contains an illegal character (for example, from json_decode () or SimpleXML).

Example #1 Variable Attribute example

 PhpclassFoo {var $bar= ' I am bar. '; var $arr=Array(' I am A. ', ' I am B. ', ' I am C. ')); var $r= ' I am r. ';}$foo=Newfoo ();$bar= ' Bar ';$baz=Array(' foo ', ' Bar ', ' baz ', ' Quux '));Echo $foo-$bar. "\ n";Echo $foo-$baz[1]. "\ n";$start= ' B ';$end= ' AR ';Echo $foo->{$start.$end} . "\ n";$arr= ' arr ';Echo $foo-$arr[1]. "\ n";Echo $foo->{$arr}[1]. "\ n";?>

The above routines will output:

I am Bar. I am Bar. I am Bar. I am R. I am B.

Example #2 Variable Attribute example

 
  PHP  //can even add more Dollar signs  $Bar = "a";   $Foo = "Bar";   $World = "Foo";   $Hello = "World";   $a = "Hello";   $a // Returns Hello  $$a//ReturnsWorld  $$$a//Returns Foo  $$$$a//Returns Bar  $$a//Returns A    $$$$$$a//Returns Hello  $$$$$$$a//  Returns World  //... and so on ...//?>

Example #3 Variable Attribute example

 
  PHP//  Given These variables ... $nameTypes    Array ("First", "Last", "Company"); $name _first   = "John"; $name _last    = "Doe"; $name _company = "Php.net"; // Then this loop is ... foreach ($nameTypesas$type)   print ${"name_$type"}. "\ n"; // ... equivalent to this print statement. Print "$name _first\$name _last\$name _company\ n";? >

The above routines will output:

johndoephp. netjohndoephp. Net

Excerpt from: http://php.net/manual/zh/functions.variable-functions.php

http://www.bkjia.com/PHPjc/1075467.html www.bkjia.com true http://www.bkjia.com/PHPjc/1075467.html techarticle PHP Learning-mutable variables-variable variable variables sometimes it is convenient to use variable variable names. That is, the variable name of a variable can be set and used dynamically. A pu ...

  • 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.