In PHP, superglobal variables are built-in, predefined arrays that are always accessible from anywhere within your script, making them “super” global. These variables store and manage various types of information, such as form data, server details, session data, and more. Here’s an overview of the main PHP superglobals and their usage:
1. $_GLOBALS
- Purpose: Stores all global variables in PHP.
- Usage: You can access any global variable from anywhere in the script. This is helpful when you want to access variables defined outside a function inside that function.
- Example:
$name = “John”; function display_name() { echo $_GLOBALS[‘name’]; // Accesses the global $name variable } display_name(); // Outputs “John”
2. $_SERVER
- Purpose: Contains server and environment information, such as headers, paths, and script locations.
- Commonly Used Keys:
- $_SERVER[‘PHP_SELF’]: The file path of the currently executing script.
- $_SERVER[‘SERVER_NAME’]: The server’s hostname.
- $_SERVER[‘REQUEST_METHOD’]: The request method used to access the page (GET, POST, etc.).
- $_SERVER[‘REMOTE_ADDR’]: The IP address from where the user is viewing the current page.
- Example:
echo $_SERVER[‘SERVER_NAME’]; // Outputs the server name echo $_SERVER[‘REQUEST_METHOD’]; // Outputs the request method used, e.g., POST
3. $_GET
- Purpose: Contains data sent to the script via the URL query string (GET method).
- Usage: Useful for collecting data sent in a URL, such as in search forms.
- Example:
// URL: http://example.com/page.php?name=John&age=30 echo $_GET[‘name’]; // Outputs “John” echo $_GET[‘age’]; // Outputs “30”
4. $_POST
- Purpose: Stores data sent to the script via HTTP POST requests, commonly used for form submissions.
- Usage: Preferred for sending sensitive information as it doesn’t expose data in the URL.
- Example:
if ($_SERVER[“REQUEST_METHOD”] == “POST”) { echo $_POST[‘username’]; // Accesses form data with name=”username” }
5. $_FILES
- Purpose: Handles file uploads.
- Usage: Contains file upload data, including the file name, type, size, temporary location, and any upload errors.
- Example:
if ($_FILES[‘uploaded_file’][‘error’] == UPLOAD_ERR_OK) { $temp_location = $_FILES[‘uploaded_file’][‘tmp_name’]; $name = $_FILES[‘uploaded_file’][‘name’]; move_uploaded_file($temp_location, “uploads/$name”); // Moves file to the “uploads” directory }
6. $_COOKIE
- Purpose: Contains data sent to the script via HTTP cookies.
- Usage: Used to retrieve stored cookie values set previously by the server.
- Example:
setcookie(“user”, “John Doe”, time() + 3600); // Sets a cookie echo $_COOKIE[‘user’]; // Outputs “John Doe” if the cookie is set
7. $_SESSION
- Purpose: Stores session data for individual users. Unlike cookies, session data is stored on the server.
- Usage: Useful for maintaining user-specific data across multiple pages, such as login information.
- Example:
session_start(); $_SESSION[‘username’] = “JohnDoe”; // Sets a session variable echo $_SESSION[‘username’]; // Outputs “JohnDoe”
8. $_REQUEST
- Purpose: Contains data from both $_GET, $_POST, and $_COOKIE. It’s a catch-all for form submissions or query parameters but is less specific than using $_GET or $_POST.
- Usage: Convenient when you’re unsure if the data comes via GET or POST, but using $_GET or $_POST is often better for clarity.
- Example:
echo $_REQUEST[‘name’]; // Will work with either GET or POST requests
9. $_ENV
- Purpose: Contains environment variables. Often used for configuration settings specific to the environment, like database credentials or API keys.
- Usage: Data in $_ENV usually comes from the server’s environment variables or .env files in modern PHP frameworks.
- Example:
echo $_ENV[‘PATH’]; // Outputs the system’s PATH environment variable
10. $argc and $argv
- Purpose: Used for handling command-line arguments in PHP scripts. $argc provides the number of arguments, and $argv provides an array of arguments.
- Usage: Useful when executing PHP scripts via the command line.
- Example:
// Command: php script.php arg1 arg2 echo $argc; // Outputs 3 (script name + 2 arguments) echo $argv[1]; // Outputs “arg1”
Summary
Superglobals are an essential part of PHP, providing direct access to data sources like form input, cookies, sessions, and server environment. Each superglobal has its specific purpose, and understanding them enables you to build dynamic, data-driven PHP applications. Remember to handle superglobals with care, especially by validating and sanitizing input from sources like $_POST and $_GET to maintain security and data integrity.