PHP variables in basic PHP tutorials

Source: Internet
Author: User
Many friends sometimes have an uncertain problem with variables when writing PHP programs, and many problems are caused by improper variable processing. Variable refers to the amount of data that can change during program execution. A variable is identified by a name (variable name. The system allocates a storage unit for each variable in the program. The variable name is essentially the name of the computer memory unit. Therefore, you can use the variable name to access data in the memory.

I. Declaration and use of variables


You do not need to declare variables before using variables in PHP. you only need to assign values to variables. The variable name in PHP is represented by $ and the identifier. the variable name is case sensitive.

Assign a value to a variable refers to a specific data value. for string and numeric variables, you can assign a value through "=. Format:

 

When assigning values to variables, follow the variable naming rules. The following variables are valid:

 

The following variables are invalid:

 

In addition to direct value assignment, there are two ways to declare or assign values to variables.

1. value assignment between variables

Definition: The value assignment between variables indicates that the last two variables use their respective memory and do not interfere with each other. The example is as follows:

 

Output:Yes.

2. Reference assignment

The concept of reference assignment starts with PHP4, that is, accessing the content of the same variable with different names. When you change the value of one of the variables, the other also changes. The '&' symbol is used to indicate reference. Example:

 "; Echo $ I; // output variable $ I?>

Output result:

hello,spcnhello,spcn 

In this example, the variable $ j is a reference to the variable $ I. When the variable $ I is assigned a value, the value of $ j also changes.


II. Scope of variables

When a variable is used, it is the rule for defining load variables. The variable must be used within the valid time range. if the variable is out of the valid range, the variable will become meaningless.

The scope of the variable is as follows:

Local variables:The variable defined on the back of the function. its scope is the function

Global variables:Variables other than all functions are defined. the scope of the variables is the entire php file, but they are unavailable within the user-defined functions. If you want to use global variables within a user-defined function, useGlobalKeyword declaration global variable

Static variables:The variable value can be retained after the function call ends. when the function returns to its scope again, the original value can be used again. Generally, after a function call is completed, the stored data value is cleared and the occupied memory space is released. When using static variables, use keywords first.StaticTo declare and change to the most. put the keyword static before the variable to be defined

Variables defined within the function. the scope of the variables is the function. if values are assigned outside the function, they are considered to be completely different. Exit the function that declares the variable. The variable and the corresponding value are cleared.

 

 From the above example, we can easily and clearly understand the gap between global and local variables in PHP...

We are glad that PHP provides server global variables for other CGI languages... these variables are automatically generated by the system when the page is called.

These global variables are included in

$ _ SERVER ($ HTTP_SERVER_VARS) (related variable services provided by the SERVER) PHP4.1.0

$ _ ENV ($ HTTP_ENV_VARS) (save related environment variables) PHP4.1.0

$ _ POST (save the variables submitted using Form post method) PHP4.1.0

$ _ GET (save the variables submitted using the Form GET/URI method) PHP4.1.0

$ _ COOKIE (save the COOKIE data obtained on the page) PHP4.1.0

$ _ SESSION (save the intra-site SESSION variable) PHP4.1.0

$ HTTP_POST_VARS (same as $ _ POST, comments are $ _ POST more efficient) PHP4.1.0

$ HTTP_GET_VARS (same as $ _ GET, comment as above.) PHP4.1.0

$ _ REQUEST (including $ _ GET, $ _ POST, $ _ COOKIE, $ _ FILES) PHP4.1.0

$ _ FILES (strictly speaking, this variable is already included in $ _ POST and $ HTTP_POST_VARS, mainly to get the file variable submitted in form post mode) PHP4.1.0

$ GLOBALS (an array that saves all global variables) PHP3.0.0

$ Php_errormsg (this global variable must be opened in php. ini: track_error = on)

The above variables are system global variables and can be directly used without declaring them. of course, your PHP version must be higher than or equal to the version number marked later by them.

When using these variables, you do not need to use the global keyword to declare them...  

 


III. variable

PHP also provides the variable concept. 

A variable is a unique variable that allows dynamic change of the name. The working principle is that the variable name is determined by the value of another variable. the implementation process is to add a dollar sign "$" before the variable ".

The following uses the variable to change the name most dynamically. First, define two variables $ change_name and $ trans. and output the value of the variable $ change_name, then use the variable to change the name of the variable $ change_name, and finally output the variable value after the name change. The instance code is as follows: 

 "; Echo $ change_name; // Output $ trans through variable?>

Output result:

transyou can see

 

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.