Get the App
SLTechnology News&Howtos  ›  Development  › 

Detailed explanation of variable types and scope in PHP

Shulou Source: shulou.com Published: 2022-06-03 15:44:40 09月15日 Update

This article mainly introduces "PHP variable types and scope detailed explanation", in daily operation, I believe many people in PHP variable types and scope detailed explanation problems have doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "PHP variable types and scope detailed explanation" doubts helpful! Next, please follow the small series to learn together!

The scope of variables in PHP can be divided into: super global (special type of global variable, directly used in local scope), global, local, static (special type of local variable)

In PHP, global variables are actually static global variables. If you don't explicitly release them by unset, global variables will not be released until the script runs out.

Local static variable refinement can be local static function variables (static variables declared in functions), local static member variables (static attributes declared in classes, shared by all class instances)

Local static variables are automatically released only at the end of the script run

Hyperglobal variables: can be accessed from any scope of a script, these are built-in PHP

The copy code is as follows:

$GLOBALS

$_SERVER

$_GET

$_POST

$_FILES

$_SESSION(persistent storage)

$_COOKIE(persistent storage)

$_REQUEST

$_ENV

Global variables: declared variables are not inside class,function,if and other language structures. If you want to use them inside class,function,if and other languages, you need to use the keyword global or super-global variable $GLOBALS.

static variable: a variable declared with the keyword static in function, the value of the static variable is retained until the end of the script

Local variables: variables declared inside class, function, if/while/for statements

1.global keywords and $GLOBALS examples

The copy code is as follows:

Tags: Variables static code global local output action type instance key keyword property method script learning member special function more structure Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Microsoft OPPO Reno Linux Docker