Introduction to variable variable names in PHP

Source: Internet
Author: User
Tags array variables variable

Sometimes mutable 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 statements:

<?php $a = "hello"; ?> 

Variable 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 hello to the name of a variable, just like the one below.

<?php $$a = "world"; ?> 

With the above two statements, two variables are defined: variable $a, "Hello" and variable $hello, the contents of which are "world". So, the following language:

<?php echo "$a ${$a}"; ?> 

Exactly the same as the output of the following statement:

<?php echo "$a $hello"; ?> 

They all output: Hello world.

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

Class properties can also be accessed through variable property names. The variable property name is obtained from the access scope of the variable that generated the call. For example, if your expression is like 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 $bar is an array, it can also be used.

Example 1 variable variable name

<?php 
  class foo { 
    var $bar = "I am bar."; 
  } 
  $foo = new foo(); 
  $bar = "bar";  
  $baz = array("foo", "bar", "baz", "quux");  
  echo $foo->$bar . " 
";  
  echo $foo->$baz[1] . " 
";  
?> 

The above example will output the following result:

I am Bar.

I am Bar.

Warning

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







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.