PHP variable types and scope _php tips

Source: Internet
Author: User

The scope of variables in PHP can be divided into: Super Global (the special type of global variables, in the local scope can be used directly), global, Local, static (is a special type of local variables)
In PHP, the global variable is actually a static global variable, and if you do not have to unset explicit release, the script runs the end global variable will be released
A local static variable subdivision can be a local static function variable (a static variable declared in a function), a local static member variable (a static property declared in a class that is shared by all class instances).
Local static variables are automatically freed only when the script is run over

Super Global variables: can be accessed in any scope of a script, which is built into PHP

Copy Code code as follows:

$GLOBALS
$_server
$_get
$_post
$_files
$_session (Persistent storage)
$_cookie (Persistent storage)
$_request
$_env

Global variables: Declared variables are not within the language structure such as class,function,if, if you want to use inside languages such as class,function,if, you need to use keyword Global or super global variable $globals

Static variables: Variables declared with the keyword static in function, the value of the 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 examples

Copy Code code as follows:
<?php
$a = 0;
function foo ()
{
Global $a;
echo $a;
}
function Foo2 ()
{
echo $GLOBALS [' a '];
}

example of difference between 2.static variable and ordinary local variable
Copy Code code as follows:
<?php
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 ();

First time output 1 second time 2
3.static keywords can also declare static properties and static methods
Static properties can be invoked only by classes, not by class instances
$this cannot be used in static methods, only static properties of classes can be accessed with self

Another piece of code that understands the static variable:

Copy Code code as follows:

<?php
Class T
{
static $v = 10;
Public Function A ()
{
static $var = 10;
$var + +;
Echo $var. "<br>\n";
}
public static function AA ()
{
Self:: $v + +;
echo Self:: $v. "<br>\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-&GT;AA ()//Output 12
$o 2-&GT;AA ()//Output 13

From the above code, it is easy to understand that if there are static variables in a class member method, even if it is a different class instance, they share the static variable, even though the static variable is not a class static member variable.

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.