PHP Global Variables - Superglobals

By Shahnail Khan Last updated : December 14, 2023

PHP Global Variables - Superglobals

In PHP, global variables are variables that can be accessed from any part of the script. Superglobals, on the other hand, are special types of global variables that are predefined by PHP and are always accessible, regardless of scope. These variables are known as "superglobals" as they can be accessed from any part of the script. Understanding and utilizing PHP superglobals is crucial for working with web applications and managing user input.

The PHP superglobal variables are:

  1. $GLOBALS: A PHP superglobal that is used to access global variables from anywhere in the script, ensuring they are accessible in both global and local scopes.
  2. $_SERVER: Holds information about the server environment and request headers. It provides details such as server name, request method, and user agent.
  3. $_REQUEST: A combined array that merges data from $_GET, $_POST, and $_COOKIE. It is not recommended to use $_REQUEST due to security concerns, but it's available for cases where data can come from multiple sources.
  4. $_POST: A superglobal used to collect form data sent with the HTTP POST method. It is commonly employed for processing sensitive information, like passwords, as it doesn't expose data in the URL.
  5. $_GET: A superglobal used to collect form data sent with the HTTP GET method or data passed through the URL. It's suitable for non-sensitive information and is often used for filtering and sorting.
  6. $_FILES: Used to collect information about uploaded files via HTTP POST. It contains details like file name, file type, and temporary file location.
  7. $_ENV: Holds information about environment variables, which are set by the web server or the server's operating system.
  8. $_COOKIE: A superglobal containing values sent to the server by the client in the form of cookies. It allows the server to persist data on the client side.
  9. $_SESSION: A superglobal used for storing session variables that can be accessed across multiple pages during a user's visit to a website. It facilitates data persistence between different requests from the same user.

In the next tutorials, we will cover each of the superglobal variables in detail.

All PHP Programs

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.