What are super global variables in PHP? how to use?
青灯夜游
Release: 2023-04-05 07:58:02
Original
7325 people have browsed it
There are some array variables predefined in PHP. These variables can be accessed anywhere in the script at any time. They are called super global variables. This article will introduce you to these super global variables and briefly understand the usage of these variables. I hope it will be helpful to you. [Video tutorial recommendation: PHP tutorial]
What are superglobals variables?
Super global variables are built-in, specially defined array variables in PHP. They can use super global variables to access information at any time and anywhere in the script. That is, regardless of the scope, information about the request or its context can be easily obtained.
Super global variables can be accessed from any function, class or any file without performing any special task like declaring any global variable etc. They are mainly used to store and retrieve information from one page to another in the application.
The following is a list of superglobal variables available in PHP:
● $ GLOBALS
● $ _ SERVER
● $ _REQUEST
● $ _GET
● $ _ POST
● $ _SESSION
● $ _COOKIE
● $ _FILES
● $ _ENV
Next, let’s introduce some of the super global variables in detail.
$ GLOBALS
$ GLOBALS is a super global variable that stores all variables declared in the script and can be used to access any location in the script any variable.
PHP stores all global variables in the array $GLOBALS[], this array has an index that holds the global variable name and can be accessed using that name.
In the above code, two global variables $x and $y are declared and assigned the values 300 and 200. Then define the function multiplication() to multiply and store the $x and $y values in another variable $z defined in the GLOBAL array.
We know that the variables $x and $y will not be accessed in the function multiplication() because they are not declared in it; but we can do so by accessing it using the $GLOBALS array variable.
When the function square() is called, the multiplication of the values of variables $x and $y is performed; then it can be displayed directly.
The multiplication result obtained because the variable $z also exists in the $GLOBALS array variable.
Output:
$ _SERVER
$ _SERVER is a PHP super global Variable that stores information about the title (header), path and script location, i.e. it stores information about the web and its requests. Some of these elements are used to obtain information from the superglobal variable $_SERVER.
$ There are many information elements used in the _SERVER variable. Some of them are listed below:
Component
Description
$ _ SERVER [ 'PHP_SELF']
Return the file name of the currently executing script.
##$ _ SERVER ['SERVER_ADDR']
Returns the IP address of the host server.
$ _ SERVER ['SERVER_NAME']
Returns the name of the host server.
$ _ SERVER ['QUERY_STRING']
If the page is accessed through the query string, the query string is returned.
The above is the detailed content of What are super global variables in PHP? how to use?. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn