PHP Global Variables – Superglobals

In PHP, global variables are variables that can be accessed from anywhere in the PHP script, including functions and classes. PHP has several predefined global variables, known as superglobals, which can be accessed from anywhere in the script without the need to declare them as global. The superglobals are:

  1. $_SERVER : contains server and execution environment information, such as the current script’s filename, request method, and client IP address.
  2. $_GET : contains data submitted through a GET request, in the form of key-value pairs in the URL query string.
  3. $_POST : contains data submitted through a POST request, in the form of key-value pairs in the HTTP request body.
  4. $_REQUEST : contains both $_GET and $_POST data, as well as any cookies sent with the request.
  5. $_SESSION : contains session data that is persisted across multiple requests.
  6. $_COOKIE : contains data stored in cookies sent with the request.
  7. $_FILES : contains information about files uploaded through a form, including the file name, size, and temporary location.
  8. $_ENV : contains environment variables that are set on the server.

All superglobals are automatically available within the script’s scope, and their values can be accessed or modified as needed. However, it is important to be careful when using superglobals, as they are accessible from anywhere in the script and can be modified by any part of the script, which could lead to unexpected behavior.

Scroll to Top