PHP variable instance details

Source: Internet
Author: User
There is a variable of another type in PHP, "variable ". A variable is a unique PHP variable that allows you to dynamically change the name of a variable. What is variable?

There is a variable of another type in PHP, "variable ". A variable is a unique PHP variable that allows you to dynamically change the name of a variable.

How variables work

This feature uses the value of one variable as the name of another variable. For example, we can set the value of $ str:
$ Name = 'str ';
So we can use

Replace name with $ str.

Name = 5;
This code is equivalent:
$ Str = 5;

Usage

Using a variable is to add a dollar sign "$" before the variable ".

Use Variable variables for arrays

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 runtime will look for the variable $ bar in the local variable range, the value is used as an attribute name of the $ foo object. It can also be used if $ bar is an array.

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.

Instance

The following example uses a variable to change the variable name. The code is as follows:

 "; Echo $ str; // output the value of $ trans through variable variables?>

Instance details:

First define two variables $ str and $ trans, and output the value of the variable $ str, then use the variable to change the name of the variable $ str, and finally output the variable value after the name is changed.

The running result is as follows:

The above is a simple usage of variable variables. in the next section, we will introduce "PHP pre-defined variables"

Related video tutorial recommendations: php1.cn solitary 9 cheap (4)-php video tutorial variable: provides a new idea for creating new variables

The above is a detailed description of PHP variable instances. For more information, see other related articles in the first PHP community!

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.