Local variable: A variable defined inside a function that is scoped to the function definition.
Global variable: A variable defined outside a function whose scope is defined at the end of the file.
In PHP, functions are treated as separate patches, so local variables overwrite global variables, even if they are in local variables and define variables that do not have the same global variables, they are overwritten.
As follows:
$a = 5;
$b = 3;
function T ()
{
echo $a-$b;//Output 0
}
To use global variables directly in a function can be declared through the Global keyword or through a custom $globals array in PHP
function T ()
{
Global $a, $b;
echo $a-$b;
}
function T ()
{
echo $GLOBALS [' a ']-$GLOBALS [' B '];
}
Static variable: Defines a static variable through static, such as:
function test ()
{
Static $t;
$t + +;
return $t;
}
for ($i =0; $i <6; $i + +)
{
echo Test (). " <br/> ";
}
Output 1 2 3 4 5
That is, after the first initialization of a static variable, the value of the variable is always in memory and will not be written off with the function.
Note: If you unset () a static variable in the function, this static variable will be destroyed inside the function. However, when this function is called again, the static variable is restored to the value before it was last destroyed.
Local variables and global variables in May 18:php