PHP variable type and scope _php tutorial

Source: Internet
Author: User
The scope of variables in PHP can be divided into: Super Global (the special type of global variables, can be used directly in local scope), global, Local, static (is the special type of local variables)
In PHP, the global variable is actually a static global variable, and if it is not explicitly released by unset, then the global variable will be released when the script runs out.
Local static variable subdivision can be local static function variable (static variable declared in function), local static member variable (static attribute declared in class, shared by all class instances)
Local static variables are automatically freed only by the end of the script run

Hyper Global variables: can be accessed in any scope of a script, these are PHP built-in
Copy the Code code as follows:
$GLOBALS
$_server
$_get
$_post
$_files
$_session (Persistent storage)
$_cookie (Persistent storage)
$_request
$_env
Global variables: The declared variables are not inside the language structure such as class,function,if, and if you want to use them inside class,function,if and other languages, you need to use the keyword Global or super global variables $globals

Static variable: A variable declared with the keyword static in function, the value of a static variable remains until the end of the script
Local variables: Variables declared inside a struct statement such as Class,function,if/while/for

1.global Keywords and $globals instances
Copy the code code as follows:!--? php $a = 0;
function foo ()
{
Global $a;
echo $a;
}
Function Foo2 ()
{
echo $GLOBALS [' a '];
}
2.static variable and ordinary local variable distinguish instance
Copy the code code as follows:!--? php function foo1 ()
{
$var = 0;
$var + +;
return $var;
}
Echo foo1 ();
Echo foo1 ();
//output is 1
function foo ()
{
Static $var = 0;
$var + +;
return var;
}
Echo foo ();
Echo foo ();
//First Output 1 second 2
3.static keywords You can also declare static and static methods
Static properties can only be called by the class, not by class instances
Static methods cannot use $this. The static property of a class can only be accessed with self

Another piece of code that understands the static variable:
Copy the Code code as follows:
Class T
{
static $v = 10;
Public Function A ()
{
static $var = 10;
$var + +;
Echo $var. "
\ n ";
}
public static function AA ()
{
Self:: $v + +;
echo Self:: $v. "
\ n ";
}
}
$o 1 = new T ();
$o 1->a ();//Output 11
$o 2 = new T ();
$o 2->a ();//Output 12
T::AA ();//Output 11
$o 1->aa ();//Output 12
$o 2->aa ();//Output 13

From the code above, if there is a static variable in a class member method, even if it is a different class instance, they will share the static variable, although this static variable is not a class static member variable, which is confusing.

http://www.bkjia.com/PHPjc/740659.html www.bkjia.com true http://www.bkjia.com/PHPjc/740659.html techarticle The scope of variables in PHP can be divided into: super-Global (special type of global variables, can be used directly in local scope), global, Local, static (is the special type of local variables) in PHP ...

  • 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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.