Introduction to variable scopes and Super global variables in php

Source: Internet
Author: User
Tags php script php tutorial

I. Variable assignment

You can assign values to variables after they are declared. There are two methods: value assignment and reference assignment.
1. Value assignment
$ Color = "red ";
$ Sum = 12 + "15";/* $ sum = 27 */

2. If you want two variables to point to the same copy of a value, you must assign values by referencing them.
Reference assignment
The created variable is the same as that referenced by another variable. If multiple variables reference the same content, modifying any of them will reflect the rest of the variables.
Example:
<? Php Tutorial
$ Value1 = "hello ";
$ Value2 = & value1;/* $ value1 and $ value2 both equal "hello ".*/
$ Value2 = "goodbye";/* $ value1 and $ value2 both equeal "goodbye ".*/
?>

II. Scope of variables
Variables can be declared at any position of the php script, but the location of the declared variables will greatly affect the scope of the access variables. The accessible scope is called scope ).
Scope of php variable 4:
△Local variable
△Function parameters
△Global variable
△Static variable
1. Local variables
The declared variable in the function is considered a local variable and can only be referenced in the function. When the declared variable function is exited, the variable and the corresponding value are revoked. This eliminates the possibility that variables that cause global access are intentionally or unintentionally modified.

$ X = 4;
Function assignx (){
$ X = 0;
Print "$ x inside function is $ x. <br> ";
}
Assignx ();
Print "$ x outside of function is $ x. <br> ";
The code execution result is:
$ X inside function is 0.
$ X outside function is 4.

2. Function parameters
Like other programming languages, any function that accepts parameters must declare these parameters in the function header. Although these parameters accept values outside the function, they cannot be accessed after exiting the function. (Except for parameters passed by reference)
For example:
Function x10 ($ value ){
$ Value = $ value * 10;
Return $ value;
}
After the function is executed, the parameter is about to be revoked.

3. Global variables
Unlike local variables, global variables can be accessed anywhere in the program. When changing a global variable in a function, you need to display the life of the variable as a global variable in this function, as long as you add global before the variable in the function.
For example:
$ Somevar = 15;
Function addit (){
Global $ somevar;
$ Somevar ++;
Print "somevar is $ somevar ";
}
Addit ();
$ Somevar: The value displayed is 16. However, if global $ somevar is removed, the variable $ somevar is implicitly set to 0 and 1 is added. The value displayed at the end is 1.
Another way to declare global variables is to use the $ global array of php, as shown below:
$ Somevar = 15;
Function addit (){
$ Globals ['somevar '] ++;
}
Addit ();
Print "somevar is". $ globals ['somevar '];
Return value: somevar is 16.

4. Static variables
Static scope. Function parameters of common variables are revoked at the end of the function, but static variables do not lose the value when the function exits, and the value can be retained when the function is called again. You can declare a static variable by adding the keyword static before the worker beam.
Static $ somevar;
Consider an example:
Function keep_track (){
Static $ count = 0;
$ Count ++;
Print $ count;
Print "<br> ";
}
Keep_track ();
Keep_track ();
Keep_track ();
Keep_track ();
If $ count is not specified as static (corresponding, $ count is a local variable), the output will be
1
1
1
1
Because $ count is static, it retains the previous value each time the function is executed. The output is as follows:
1
2
3
4
Static scopes are useful for recursive functions. A recursive function is a powerful programming concept. It is a function that can call itself repeatedly until a certain condition is met.

5. php Super global variables
Php provides many useful pre-defined variables that can be accessed at the person and location where the script is executed and used to provide a large amount of environment-related information. You can use these variables to obtain detailed information about the current user session, user operating system environment, and local operating environment. Php creates some variables, while the availability and value of many other variables depend on the operating system and web services.

Output all predefined variables:
Foreach ($ _ server as $ var => $ value ){
Echo "$ var => $ value <br> ";
}
Show the user's IP address:
Print "hi! Your ip address is ". $ _ server ['remote_addr '];

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.