Global variables: Variables defined in the main program (outside the function), can only be used in the main program, cannot be called inside the function
Background: Solving the problem of calling global variables inside a function
Workaround:
1. Declare global variables inside a function
<? PHP Public $var=1; function Fun () { echo$GLOBALS[' var ']; }? >1
2. Using $globals hyper-global arrays
<? $var=1; function Fun () { echo$GLOBALS[' var ']; } Fun ();? >1
Principle Analysis:
$GLOBALS [' var '] is the external global variable itself
The global $var is a reference or pointer to an external $var variable with the same name, not a true assignment
Verify:
<?php
$var 1 = 1;
function Test () {
Global $var 1;
Unset ($var 1);
}
Test ();
echo $var 1;
?>
The description removes only variables inside the function (that is, references to external variable aliases), and external variables do not affect
Again such as:
<? $var 1 = 1functionunset($GLOBALS[' var1 ' Echo $var 1 ?>
The variable does not exist to delete the global variable
In-depth analysis:
<?PHPfunctionTest_global () {Global $var 1,$var 2; $var 2=&$var 1;//reference to a new point, only used with the inside of the function, the external global variable does not affect only the assignment will change the valueEcho $var 2; 5}functiontest_globals () {$GLOBALS[' Var3 '] =&$GLOBALS[' Var1 ']; //equivalent to a reference to the original global variable, the value will change} $var 1= 5; $var 2=$var 3= 0; Test_global (); Output 5Print $var 2." \ n "; Output 0test_globals (); Print $var 3." \ n "; Output 5?>Output5 0 5
Summarize:
1.global $var is a reference or pointer to a variable of the same name as an external $var, and only the assignment changes the value of the global variable, if it is a reference ($var 2 =& $var 1;) applies only to the inside of the function
2. $GLOBALS [' var '] is the external global variable itself and can be called inside the function using the $globals array
3. Scope: The role of global is to define globals, but this global variable is not applied to the entire Web site, but is applied to the current page, including all files of include or require.
$GLOBALS Super Global Array for anywhere
4. The global variable defined in the function body can be used outside the body, and the global variable defined outside the function body cannot be used within the function body.
5. You can call all variables and constants in the system and the values of the property variables in the objects and objects using the $globals Hyper-global array
$GLOBALS [' ECS '] + ECS object $ecs called
$GLOBALS [' _server ']=> $_server global variable array call the Hyper global variable $_server array
$GLOBALS [' ECS '] [' db_name '] and ECS object calls the variables defined inside the ECS objects $db _name
$GLOBALS [' ECS ']->table (' Shop_config ') ECS object's Table method () calls the table function defined inside the ECS object
$_globals What is the difference between global variables defined by a Hyper global array and global?