Home >Backend Development >PHP Tutorial >Detailed explanation of the usage of 9 predefined superglobal variables in PHP
Many predefined variables in PHP are "superglobal", which means they are available throughout the entire scope of a script. They can be accessed within a function or method without executing global $variable;.
These superglobal variables are:
$GLOBALS
1, $_SERVER
$_SERVER super global variable contains the variables created by the web server Information, which provides information about server and client configurations and the current request environment. Depending on the server, the variable value and number of variables in $_SERVER will be different, but generally you can find the variables defined in the CGI1.1 specification. These include: $_SERVER['HTTP_REFERER']; The URL of the page that leads the user to the current location;2, $_GET
$_GET super global variable contains information about the parameters passed using the GET method. If the request URL is [url]http://www.example.com /index.html?cat=apache&id=157[/url], you can use the $_GET super global variable to access the following variables: $ _GET['cat'] = "apache";$_GET['id'] = "157";By default, to access variables passed through the GET method, $_GET Super global variables are the only way.3. $_POST
$_POST super global variable contains information about the parameters passed by the POST method. The code is as follows:
<form caction="subscribe.php" method="post"> <p> Email address : <br> <input type="text" name="email" size="20" maxlength="so" value=""> </p> <p> Password : <br> <input type="password" name="pswd" size="20" maxlength="15" value=""> </p> <p> <input type="submit" name="subscribe" value="subscribe!"> </p> </form>Through the script subscribe.php, you can use the following
POST variable :
$_POST['email'] = " jason@example.com "; $_POST['pswd'] = "rainyday"; $_POST['subscribe'] = "subscribe!";Like $_GET, by default, the $_POST super global variable is the only way to access POST variables.
4, $_COOKIE
$_COOKIE super global variable stores the information passed to the script through the HTTP cookie. These cookies are generally set by a previously executed PHP script through the5, $_FILES
$_FILES super global variable contains information about the data uploaded to the server through the POST method. This super global variable is different from other variables. It is atwo-dimensional array, containing 5 elements. The first subscript indicates the File upload element name of the form; the second subscript is one of five predefined subscripts, which describe a certain attribute of the uploaded file:
$_FILES['upload-name']['name']; 从客户端向服务器上传文件的文件名; $_FILES['upload-name']['type']; 上传文件的MIME类型,这个变量是否赋值取决于浏览器的功能。 $_FILES['upload-name']['size']; 上传文件的大小(以字节为单位); $_FILES['upload-name']['tmp_name']; 上传之后,将此文件移到最终位置之前赋予的临时名。 $_FILES['upload-name']['error']; 上传状态码。尽管这个变量的名为 error ,但实际上在成功的情况下也会填写这个变量。它有五个可能的值: UPLOAD_ERR_OK 文件成功上传 UPLOAD_ERR_INI_SIZE 文件大小超出了 upload_max_filesize 指令所指定的最大值。 UPLOAD_ERR_FORM_SIZE 文件大小超出了MAX_FILE_SIZE 隐藏表单域参数(可选)指定的最大值。 UPLOAD_ERR_PARTIAL 文件只上传了一部分 UPLOAD_ERR_NO_FILE 上传表单中没有指定文件
6, $_ENV
$_ENV super global variable provides information about the server environment where PHP parses. Variables in this array include: $_ENV['HOSTNAME'] Server's host name$_ENV['SHELL'] System shell7, The $_REQUEST
$_REQUEST super global variable is an all-rounder that records variables passed to the script through various methods, especially GET, POST and COOKIE. The order of these variables does not depend on the order in which they appear in the send script, but on the order specified by the variables_order configuration directive. It is recommended to use this super variable sparingly because it is not safe enough.8, $_SESSION
$_SESSION super global variable contains information related to all sessions. Registering session information gives you the convenience of being able to reference it throughout your site without having to explicitly pass the data via GET or POST.9, $GLOBALS
$GLOBALS The super global variable array can be considered a superset of super global variables, including all variables in the global scope. Execute the following code to view all variables in $GLOBALS.print '<pre class="brush:php;toolbar:false">'; print_r ($GLOBALS); print '';
The above is the detailed content of Detailed explanation of the usage of 9 predefined superglobal variables in PHP. For more information, please follow other related articles on the PHP Chinese website!