This article summarizes the information about the use of global static and $globals in PHP and the difference between the need to understand their three differences of friends can refer to.
1.global works throughout the page.
Example 1
| The code is as follows |
Copy Code |
|
Global $var 1, $var 2; |
is a reference to an external variable with the same name, and the scope of the variable itself remains within the function body. Changing the values of these variables, the external variable with the same name also changes naturally. But once & is used, the variable will no longer be referenced with the same name.
| The code is as follows |
Copy Code |
|
$var 1 = 1; $var 2 = 2;
function Test () {
Global $var 1, $var 2;
Scope of action in the function body
$var 1 = 3;}
Test ();
echo $var 1;
?> |
The result is 3.
Because it is the same name reference
PHP global variables defined within the function body can be used outside the body, and global variables defined outside the function body cannot be used within the function body.
| The code is as follows |
Copy Code |
|
$glpbal $a; $a = 123; function f () {echo $a;//Error,} |
Take a look at the following example
| The code is as follows |
Copy Code |
|
function f () {global $a; $a =123;} f (); echo $a; Correct, you can use |
2.static works only within function and class.
Global and $globals use are basically the same, but are very different in actual development.
Global in the function produces a pointer to the function external variable alias variable, but not the real function external variable, one but changed the alias variable point of address, there will be some unexpected situation,
Example Example 1.
| The code is as follows |
Copy Code |
|
/*
*author:ajax123
*qq:283400245
*/
Class person{
Static$name= "ajax123";//static declaring static properties
Static$age=25;//static declaring static properties
static$address= "Beijing";//static declaring static properties
function Song () {
Echo ' My name is: ' Self:: $name. "
";//Inside Class: Accessing static properties through the Self class
echo "I am". Self:: $age. "
";//Inside Class: Accessing static properties through the Self class
Echo ' I live in '. Self:: $address. "
";//class internal: Access static properties through the Self class
}
}
Echoperson:: $name. "
";//class external: Access static properties by class name person
Echoperson:: $age. "
";//class external: Access static properties by class name person
Echoperson:: $address. "
";//class external: Access static properties by class name person
?>
|
$GLOBALS [] The actual call is an external variable, and the function remains consistent inside and outside
Example #1 $GLOBALS Example
| The code is as follows |
Copy Code |
|
function Test () {
$foo = "local variable";
Echo ' $foo in global scope: '. $GLOBALS ["foo"]. "N";
Echo ' $foo in current scope: '. $foo. "N";
}
$foo = "Example content";
Test ();
?> |
The output of the above routines is similar to the following:
$foo in global scope:example content
$foo in current scope:local variable
http://www.bkjia.com/PHPjc/628908.html www.bkjia.com true http://www.bkjia.com/PHPjc/628908.html techarticle This article summarizes the information about the use of global static and $globals in PHP and the difference between the need to understand their three differences of friends can refer to. 1.global works throughout the page. Example 1 code such as ...