Variable range of PHP for front-end learning, front-end php variable
* Directory [1] range span [2] function range [3] global [4] predefined variable [5] before the hyperglobal variable
The variable range is the context defined by the variable (that is, its effective range ). In javascript, there is no variable range concept, and similar scope may be used. However, because javscript uses the lexical scope, it refers to the position where the variable is declared. php does not have a variable declaration. When a variable is assigned for the first time, it is equivalent to declaring a variable. Therefore, they are different. This article will introduce the variable range in php in detail
Range Span
Most PHP variables have only one separate range. This independent range span also contains the files introduced by include and require.
The variable $ a will take effect in the included file B. inc.
<?php$a = 1;include 'b.inc';?>
Function range
In a user-defined function, a local function range is introduced. Any variable used in the function will be limited to the local function by default.
The following script does not have any output, because the echo statement references a local version variable $ a and is not assigned a value within this range.
<?php$a = 1; /* global scope */function Test(){ echo $a; /* reference to local scope variable */}Test();?>
Global keyword
The output of the following script is "3 ". After the global variables $ a and $ B are declared in the function, all references to any variable will point to its global version. PHP has no limit on the maximum number of global variables that a function can declare.
<?php$a = 1;$b = 2;function Sum(){ global $a, $b; $b = $a + $b;}Sum();echo $b;?>
The second way to access variables globally is to use a special PHP custom $ GLOBALS array.
GLOBALS is an associated array. Each variable is an element. The key name corresponds to the variable name and the value corresponds to the variable content. GLOBALS exists globally because GLOBALS is a super global variable.
<?php$a = 1;$b = 2;function Sum(){ $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];}Sum();echo $b;?>
Predefined Variables
For all scripts, PHP provides a large number of predefined variables. These variables represent all external variables as built-in environment variables and the error message as the return header.
The list of pre-defined variables is as follows:
Super global variables-Super global variables are built-in variables that are always available in all scopes $ GLOBALS-reference all variables available in the global scope $ _ SERVER-SERVER and execution environment information $ _ GET -http get variable $ _ POST-http post variable $ _ FILES-HTTP File Upload variable $ _ REQUEST-HTTP Request variable $ _ SESSION-Session variable $ _ ENV-environment variable $ _ COOKIE-HTTP Cookies $ php_errormsg-previous error message $ HTTP_RAW_POST_DATA-native POST data $ http_response_header-HTTP Response Header $ argc-number of parameters passed to the script $ argv-parameters passed to the script array
Most pre-defined variables are not super-global variables. They need to use the 'global' keyword to make them valid in the local area of the function.
Super global variable
Super global variables are valid in any range and do not need to be declared by 'global '.
The following is a list of super global variables.
$GLOBALS$_SERVER$_GET$_POST$_FILES$_COOKIE$_SESSION$_REQUEST$_ENV
<? Phpfunction test_global () {// predefined variable global $ HTTP_POST_VARS; echo $ HTTP_POST_VARS ['name']; // super global variable echo $ _ POST ['name'];}?>