PHP Summary of Predefined Hyperglobal Array Variables

  • 2021-10-27 06:43:56
  • OfStack

This article illustrates the PHP predefined hyper-global array variables. Share it for your reference, as follows:

1. PHP predefined super-global variable array

Characteristics:

a. Special array, no difference in operation mode

b. No declaration, php script exists by default, because it is not defined in php, so the custom variable should avoid having the same name as the predetermined global variable

c. It takes effect automatically in the global scope, that is, it can be used directly in the function without using global keyword access

2. Hyperglobal Array

a. $_SERVER Server variable

$_ SERVER is an array containing information such as header, path, and script location

Example:


foreach($_SERVER as $key => $value){
echo '$_SERVER['+.$key+']='.$value.'<br>';
}

b. $_ENV Environment variable

The contents of the $_ ENV array are converted from an environment variable in the server where PHP is located to an PHP global variable when the PHP parser is running

Example:


foreach($_ENV as $key => $value){
echo '$_ENV['+.$key+']'.$value.'<br>';
}

c. $_GET Url GET variable

The $_ GET array is also an array of hyper-global variables. The variables passed through the Url Get method constitute an array of external variables, that is, parameters passed in the server page through the $_ GET hyper-global array Url or the form GET

Example:

http://www.xxx.com/index.php?id=1 & name=lin


echo 'Id='.$_GET['id'].'<br>';
echo 'Name='.$_GET['name'].'<br>';

Or:


print_r($_GET);

d. $_POST HTTP POST Variable

The $_ POST array is an array of variables passed through the HTTP POST method. Both the $_ POST and $_ GET arrays can hold the variables submitted by the form

Example:


<form action='save.php' method='post'>
<input type='text' name='name'/>
<input type='text' name='id'/>
</form>

foreach($_POST as $key=>$value){
echo $key.'='$value.'<br>';
}

e. $_REQUEST request variable

This associative array contains all the contents of $_ GET $_ POST and $_ COOKIE. If the form is submitted through post of form, it will be obtained through $_ POST, and if it is sent through GET, it will be obtained through $_ GET. $_ REQUEST doesn't care whether it is POST or GET, that is, $_ REQUEST can get data from get or post but it is slow.

f. $_FILES HTTP file upload variable

When uploading a file using the form file input field, it must be submitted using post but cannot be obtained on the server side through the $_ post file but through $_ FILES $_ FILES is a 2-dimensional array containing 5 child elements.

g. $_COOKIE HTTP cookies

The $_ COOKIE hyper-global array is submitted to script variables via the HTTP cookies method, through which cookies is passed by a previously executed PHP script setCookie() Function is set to the browser of the client, php script will automatically convert cookie into a variable after obtaining cookie from the client, and the specified cookie value can be accessed through $_ COOKIE hyper-global array and cookies name.

h. $_SESSION session variable

Session control is to use session to track users on the server side, and when using session in the server page, session_start() You can use the $_ SESSION array host global variable when session is turned on by the.

i. $_ENV0 global

$GLOBALS is an array composed of all defined global variables, and the variable name is the index of the array, which is valid in all scripts. It is not necessary to use the keyword global to access in functions or methods in objects. If you declare global variables outside functions, you can use $_ GLOBALS array instead of global keyword

Example:


$a=1;$b=2;
function $sum(){
$GLOBALS['b']=$GLOBALS['a']+$GLOBALS['b'];
}
$sum();
echo $b;

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of PHP Network Programming Skills", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this paper is helpful to everyone's PHP programming.


Related articles: