Notes for using PHP variable names for arrays and classes

Source: Internet
Author: User
This article gives a detailed analysis of the issues that need to be paid attention to when using PHP variable names for arrays and classes, if you need a friend, sometimes variable names can bring great convenience to programming. That is to say, the variable name can be dynamically named and used. Generally, variables are named using the following statement:
$ A = 'hello ';
A variable name uses the value of a variable as the name of the variable. In the above example, by using two $ symbols, you can set hello as a variable name, as shown below.
$ A = 'world ';
Using the preceding two statements, two variables are defined: the variable $ a, which contains "hello" and the variable $ hello, and the variable "world ". The following language:
Echo "$ a $ {$ }";
The output is exactly the same as that of the following statement:
Echo "$ a $ hello ";
They all output: hello world.
However, to use the variable name of the array, you need to solve the ambiguity problem. That is, if you write $ a [1], the parser needs to understand whether you mean to treat $ a [1] as a variable, we still need to regard $ a as a variable. [1] refers to the index of this variable. The syntax for solving this ambiguity is: $ {$ a [1]} is used in the first case, and $ {$ a} [1] is used in the second case.

Class attributes can also be accessed through variable attribute names. The variable property name is obtained from the access range of the variable where the call is generated. For example, if your expression is like this: $ foo-> $ bar, the variable $ bar will be searched in the local variable range during running, the value is used as an attribute name of the $ foo object. It can also be used if $ bar is an array.

The code is as follows:


Class foo {
Var $ bar = 'I am bar .';
}
$ Foo = new foo ();
$ Bar = 'bar ';
$ Baz = array ('foo', 'bar', 'Baz', 'quux ');
Echo $ foo-> $ bar. "\ n ";
Echo $ foo-> $ baz [1]. "\ n ";
?>


The above example will output the following results:

The code is as follows:


I am bar.
I am bar.


Note: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 dynamically named.

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.