PHP variable variable name _php tutorial

Source: Internet
Author: User
Sometimes variable names can be a great convenience for programming. This means that variable names can be dynamically named and used. The usual variables are named by the following statements:

 
  
  
  1. Php
  2. $a' Hello '
  3. ?>

A mutable variable name refers to the use of a variable's value as the name of the variable. In the example above, by using two $ symbols, you can set the hello to a variable name, as in the following.

 
  
  
  1. Php
  2. $$a' World '
  3. ?>

With the above two statements, two variables are defined: The variable $ A, the contents are "Hello" and the variable $hello, the content is "world". So, the following language:

 
  
  
  1. Php
  2. ?>

Exactly the same as the output of the following statement:

 
  
  
  1. Php
  2. ?>

They all output: Hello world.

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.

Example 1 variable variable name

 
 
  1. PHP
  2. class Foo {
  3. var $Bar = ' I am bar '. ;
  4. }
  5. $foo = new foo ();
  6. $Bar = ' bar ';
  7. $Baz = array(' foo ', ' Bar ', ' baz ', ' Quux ');
  8. echo $foo->$bar. "N";
  9. echo $foo->$baz [1]. "N";
  10. ?>

The above example will output the following results:

I am Bar.

I am Bar.

Warning

Note that 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.

http://www.bkjia.com/PHPjc/371672.html www.bkjia.com true http://www.bkjia.com/PHPjc/371672.html techarticle sometimes variable names can be a great convenience for programming. This means that variable names can be dynamically named and used. Usually variables are named by the following statement:? PHP $ ...

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