PHP variable name usage

Source: Internet
Author: User
Sometimes variable names bring great convenience to programming. that is to say, variable names can be dynamically named and used. generally, variables are named using the following statements. the instance code is as follows :? Php... sometimes variable names bring great convenience to programming. that is to say, variable names can be dynamically named and used. generally, variables are named using the following statement. The instance code is as follows:

 

Variable variable name refers to using the value of a variable as the name of the variable. in the above example, by using two $ symbols, you can set hello to the name of a variable, the instance code is as follows:

 

Through the preceding two statements, two variables are defined: the variable $ a, which contains "hello" and the variable $ hello, and the variable "world". therefore, the following code:

 

The output is exactly the same as that of the following statement. The code is as follows:

 

They all output: hello world.

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 or $ 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.

Example 1 variable name: the code is as follows:

 $ Bar. "n"; echo $ foo-> $ baz [1]. "n"; // The above example will output the following result: // I am bar. // I am bar.?>

Warning: variable names cannot be used for Super Global array variables in PHP functions and classes. variable $ this is also a special variable that cannot be dynamically named.

Discussion on PHP variable security

Variable variables are a very convenient feature of PHP. as mentioned in the manual, variable variables mean that variable names can be dynamically set!

So what security issues will occur when the variable name can be dynamically set? Let's take a look:

 

This code is easy to understand. the type of the variable is character-type phpinfo. the variable is dynamically added with (), so the variable is changed to the phpinfo function for dynamic execution!

Following the same principle, we reference the example of variable variables in the manual:

 

This dynamic function is put into a dynamic variable. of course, this statement is not professional or variable. we will find that the phpinfo function is still executed!

If you have read the manual and the example I have given, you will surely think this is not magical. this is the syntax feature of PHP, and then we will scale this thing into one line:

 

This is a nested variable. we just enter the variable content in the previous example. In fact, a function is actually assigned to a variable, as a result, the phpinfo function is finally executed and becomes a prototype of various vulnerabilities and webshells!

You should know why I was asked to go to the PHP manual at the beginning. However, this article is over now. I also missed a point. Daniel said that security is the foundation, we haven't figured out why the variables in the previous example use single quotation marks, and the final example uses double quotation marks. if you think of this question, I think you must have great potential for security. in the future, it will be a great opportunity to be inaccurate!

The difference between single quotes and double quotes in PHP is still related to variables. the following example shows the code:

 

The content in double quotation marks is parsed by PHP syntax, and the content in single quotation marks is directly identified as a string!

So this article is really over here, so everyone should understand why the cool-man told me to read more PHP manuals and security is the basic idea.


Article address:

Reprint at will ^ please include the address of this article!

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.