PHP learning-Variable variables and variable variables-PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP learning-Variable variables,-Variable variables. PHP learning-Variable variables and variable variables-sometimes it is convenient to use variable names. That is to say, the variable name of a variable can be dynamically set and used. Learning from a common PHP -- Variable variables and -- Variable variables
Variable

Sometimes it is convenient to use a variable name. That is to say, the variable name of a variable can be dynamically set and used. A common variable is set through declaration, for example:

 

A variable obtains the value of a common variable as the variable name of the variable. In the preceding exampleHelloAfter two dollar signs ($) are used, the variable can be used as a variable. For example:

 

At this time, both variables are defined:$The content is "hello" and$ HelloThe content is "world ". Therefore, the following statement:

 

The output result is exactly the same as that of the following statement:

 

They all output: hello world.

To use variables in an array, you must solve an ambiguous problem. This is when$ A [1]The parser needs to know$ A [1]As a variable, I still want$Act as a variable and retrieve the value of [1] in the variable. The syntax for solving this problem is:$ {$ A [1]}For the second scenario$ {$ A} [1].

Class attributes can also be accessed through variable attribute names. Variable attributes will be parsed within the scope of the call. For example$ Foo-> $ barThe expression will be parsed in the local range.$ BarAnd its value will be used$ Foo. For$ BarThe same is true when it is an array unit.

You can also use curly brackets to clearly define attribute names. It is most useful when an attribute is located in an array, or when the attribute name package contains multiple parts or the attribute name package contains invalid characters (for example, from json_decode () or SimpleXML ).

Example #1 variable attributes

 $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 routine will output:

I am bar.I am bar.I am bar.I am r.I am B.

Example #2 examples of variable attributes

 

Example #3 variable attributes

 

The above routine will output:

JohnDoePHP.netJohnDoePHP.net

From: http://php.net/manual/zh/functions.variable-functions.php

Variable names are sometimes convenient to use. That is to say, the variable name of a variable can be dynamically set and used. A general...

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.