Variables are predefined in PHP

PHP provides a large number of predefined variables for any script that runs. PHP provides an additional set of arrays containing variables from Web Server, environment variables and user input. These new arrays are called Superglobal.

PHP provides a large number of predefined variables for any script that runs. PHP provides an additional set of arrays containing variables from Web Server, environment variables and user input. These new arrays are called Superglobal.

All the following variables are automatically available in all ranges.

Superglobal in PHP

You can use all of the following variables anywhere in your PHP code.

Variable Description $ GLOBALS Contains a reference to each variable that is currently available within the global scope of the script. The key of each array is the names of these global variables. $ _SERVER This is an array containing information such as header, path and script location. Entries in this array are created by the Web Server. There is no guarantee that each Web Server will provide any variable in this variable form. You follow the next section to get a list of all Server variables. $ _GET An associative array of variables passed to the current script via the HTTP GET method. $ _POST An associative array of variables passed to the current script via the HTTP POST method. $ _FILES An associative array of items uploaded to the current script via HTTP POST method. $ _REQUEST An associative array of contents of $ _GET, $ _POST and $ _COOKIE. $ _COOKIE An associative array of variables passed to the current script via HTTP cookies. $ _SESSION An associative array containing session variables available for the current script. $ _PHP_SELF A string containing the name of the PHP script file in which it is called. $ php_errormsg $ php_errormsg is a text variable of the last error message generated by PHP.

Turn $ _SERVER in PHP

The variable $ _SERVER is an array containing information such as header, path, and script location. Entries in this array are created by the Web Server. There is no guarantee that each Web Server will provide any variable in this form of Server variable.

Variable Description $ _SERVER ['PHP_SELF'] The file name of the script is currently executing, relative to the Document root. $ _SERVER ['argv'] Array parameters are passed to the script. When that script runs on the command line, it provides C-style type access to command line parameters. When called via the GET method, this variable will contain the query string. $ _SERVER ['argc'] Contains the number of command line parameters passed to the script if run on the command line. $ _SERVER ['GATEWAY_INTERFACE'] Server is using which version of CGI specification, for example 'CGI / 1.1'. $ _SERVER ['SERVER_ADDR'] The server's IP address that the script is executing. $ _SERVER ['SERVER_NAME'] Name of the host host that the script is executing. If the script is running on a virtual host, this variable will be the value defined for that virtual host. $ _SERVER ['SERVER_SOFTWARE'] Server identification string, provided in the header when responding to requests. $ _SERVER ['SERVER_PROTOCOL'] The name and version of the information protocol through which the page is requested, for example: 'HTTP / 1.0'; $ _SERVER ['REQUEST_METHOD'] Which request method is used to access the page, for example: 'GET', 'HEAD', 'POST', 'PUT'. $ _SERVER ['REQUEST_TIME'] Is the timestamp of the required start. Available from PHP 5.1.0. $ _SERVER ['QUERY_STRING'] The query string through which the page is accessed. $ _SERVER ['DOCUMENT_ROOT'] The Document root directory that the script is executing, as defined in the Server configuration file. $ _SERVER ['HTTP_ACCEPT'] Content of Accept: header from the current request, if there is one. $ _SERVER ['HTTP_ACCEPT_CHARSET'] Content of Accept-Charset: header from the current request, if there is one. For example: 'iso-8859-1, *, utf-8' $ _SERVER ['HTTP_ACCEPT_ENCODING'] Content of Accept-Encoding: header from the current request, if there is one. For example: 'gzip' $ _SERVER ['HTTP_ACCEPT_LANGUAGE'] Content of Accept-Language: header from the current request, if there is one. For example: 'en' $ _SERVER ['HTTP_CONNECTION'] The content of the Connection: header from the current request, if there is one. For example: 'Keep-Alive' $ _SERVER ['HTTP_HOST'] The content of the Host: header from the current request, if there is one. $ _SERVER ['HTTP_REFERER'] The address of the page that directs the user agent to the current page. $ _SERVER ['HTTP_USER_AGENT'] This is the string representing the user agent accessing the page. Typical examples are: Mozilla / 4.5 [en] (X11; U; Linux 2.2.9 i586). $ _SERVER ['HTTPS'] Set to a non-empty value if the script is queried via HTTPS protocol. $ _SERVER ['REMOTE_ADDR'] IP address from which the user observes the current page. $ _SERVER ['REMOTE_HOST'] Host name from which the user observes the current page. $ _SERVER ['REMOTE_PORT'] The port (port) is being used on the user's computer to communicate with the Web Server. $ _SERVER ['SCRIPT_FILENAME'] The absolute pathname of the script is currently executing. $ _SERVER ['SERVER_ADMIN'] The value provided for SERVER_ADMIN directive (with Apache) in the Web Server configuration file. $ _SERVER ['SERVER_PORT'] The port (port) on the Server computer is being used by the Server to communicate. The default setting is '80'. $ _SERVER ['SERVER_SIGNATURE'] The string contains the Server version and the host name that is added to the page created by the Server, if enabled. $ _SERVER ['PATH_TRANSLATED'] Path for the current script. $ _SERVER ['SCRIPT_NAME'] Contains the path of the current script. This variable is useful for pages that need to point to themselves. $ _SERVER ['REQUEST_URI'] URI provided to access this page, eg '/index.html' $ _SERVER ['PHP_AUTH_DIGEST'] When running under Apache as the module executing Digest HTTP authentication, this variable is Set the 'Authorization' header sent by the Client. $ _SERVER ['PHP_AUTH_USER'] When running under Apache or IIS (ISAPI on PHP 5) as a module doing HTTP authentication, this variable is set to the username provided by the user. $ _SERVER ['PHP_AUTH_PW'] When running under Apache or IIS (ISAPI on PHP 5) as the module is executing HTTP authentication, this variable is set to the password provided by the user. $ _SERVER ['AUTH_TYPE'] When running under Apache as a module implementing HTTP authenticated, this variable is set to authentication type.

Follow tutorialspoint

Previous article: Standard writing code in PHP

Next lesson: Regular Expression in PHP

4 ★ | 1 Vote