The scope of variables in PHP can be divided into: super global (a special type of global variables that can be used directly in the local scope), global, local, static (a special type of local variables)
In PHP , global variables are actually static global variables. If unset is not explicitly released, the global variables will not be released until the script is finished.
Local static variables can be subdivided into local static function variables (static declared in the function variables), local static member variables (static attributes declared in the class, shared by all class instances)
Local static variables will be automatically released only when the script is finished
Super global variables: can be accessed in any scope of a script, these are built-in PHP
Copy code The code is as follows:
$GLOBALS
$_SERVER
$_GET
$_POST
$_FILES
$_SESSION(persistent storage)
$_COOKIE(persistent storage)
$_REQUEST
$_ENV
Global variables: The declared variables are not inside language structures such as class, function, if, etc. If you want to use them inside languages such as class, function, if, etc., you need to use keywords global or super global variable $GLOBALS
Static variables: variables declared using the keyword static in function, the value of the static variable is retained until the end of the script
Local variables: variables declared inside structural statements such as class, function, if/while/for etc.
1.global keyword and $GLOBALS instance
Copy code The code is as follows:
php
$a = 0;
function foo()
{
global $a;
echo $a;
}
function foo2()
{
echo $GLOBALS['a'];
}
2. Example of the difference between static variables and ordinary local variablesCopy Code The code is as follows:
function foo1()
{
$var = 0;
$var++;
return $ var;
}
echo foo1();
echo foo1();
//The output is all 1
function foo ()
{
static $var = 0 ;
$var++;
return var;
}
echo foo();
echo foo();
//Output 1 for the first time and 2 for the second time
3.static keyword can also declare static properties and static methods Static properties can only be called by classes, but not by class instances
$this cannot be used in static methods. Static properties of a class can only be accessed with self
In addition, a piece of code to understand static variables:
Copy the code The code is as follows:
class t
{
static $v = 10;
public function a()
{
static $var = 10;
echo $var . "
n";
public static function aa()
gt;n" ;
}
}
$o1 = new t();
$o1->a();//Output 11
$o2 = new t();
$o2->a();//Output 12
t::aa();//Output 11
$o1->aa();//Output 12
$o2-> ;aa();//Output 13
From the above code, we can see that if there is a static variable in a class member method, even if they are different class instances, they will share this static variable. Although this static variable is not a class static member variable, this is easy to find. confuse.
http://www.bkjia.com/PHPjc/740659.html
www.bkjia.com
truehttp: //www.bkjia.com/PHPjc/740659.htmlTechArticleThe scope of variables in PHP can be divided into: super global (a special type of global variable, which can be used in the local scope Used directly), global, local, static (a special type of local variables) in PHP...