What are superglobals in PHP?

Superglobal Variables

Superglobal Variables in PHP are predefined global variables. Global variables are variables with global scope, which means that they can be used wherever needed – they do not need to be declared, nor do they need to be marked with global in functions. All superglobal variables are written in all-caps like, $_GLOBALS.

List of Superglobal Variables

All of the superglobal variables act as associative arrays that use a string value as a key to access values. The following is a list of superglobal variables in PHP:

  • $GLOBALS is the superglobal variable that stores all user-defined global variables. The global variable names act as keys to their values.
  • $_SERVER contains data about headers, scripts, and paths. The keys to the values in this array are predefined.
  • $_REQUEST stores data input in the form of HTTP POST, GET and Cookies. The keys to this array are defined in the HTTP requests.
  • $_POST stores data input in the form of POST requests. The keys to this array are defined in the HTTP POST request.
  • $_GET has data input in the form of GET requests. The keys to this array are defined in the HTTP GET request.
  • $_FILES is a two-dimensional associative array that contains a list of files that were uploaded to the script using the POST method. The keys to this array are the names of the fields uploading the files and the data being accessed. For example, $_FILES[fileUploaded][name] accesses the name of the file being uploaded from the fileUploaded field.
  • $_COOKIES keeps data input via HTTP Cookies. The keys to this array are defined when the cookies are set.
  • $_SESSION holds session variables. Session variables can be accessed on multiple pages. This array’s keys are defined by the users when they define session variables.
  • $_ENV contains information about the environment that PHP is running in. The keys to the values in this array are predefined.
Copyright ©2024 Educative, Inc. All rights reserved