In PHP, superglobal arrays are built-in arrays that provide access to a variety of different types of data, including user input, server information, session data, and more. These arrays are accessible from anywhere in the script without needing to use the global keyword, making them extremely useful for building web applications.
Here is a list of the primary superglobal arrays in PHP, along with their purposes:
1. $_GLOBALS
- Stores all global variables in PHP, allowing access to any global variable from any point within the script.
- Example:
$message = “Hello World”; function displayMessage() { echo $_GLOBALS[‘message’]; // Accesses the global variable } displayMessage(); // Outputs: Hello World
2. $_SERVER
- Contains information about the server environment and the execution environment of the script.
- Commonly used keys:
- $_SERVER[‘PHP_SELF’]: Returns the file path of the executing script.
- $_SERVER[‘SERVER_NAME’]: Returns the name of the server host.
- $_SERVER[‘REQUEST_METHOD’]: Returns the request method (GET, POST, etc.).
- Example:
echo $_SERVER[‘SERVER_NAME’]; // Outputs the server hostname
3. $_GET
- Stores data sent to the script via HTTP GET requests, typically from URL parameters.
- Example:
// URL: example.com/page.php?name=John echo $_GET[‘name’]; // Outputs: John
4. $_POST
- Contains data sent to the script via HTTP POST requests, commonly from form submissions.
- Example:
if ($_SERVER[“REQUEST_METHOD”] == “POST”) { echo $_POST[‘username’]; // Accesses data from a form field named “username” }
5. $_FILES
- Manages file uploads. When a file is uploaded, $_FILES stores information like file name, type, size, and temporary location.
- Example:
if ($_FILES[‘file’][‘error’] == UPLOAD_ERR_OK) { $file_name = $_FILES[‘file’][‘name’]; move_uploaded_file($_FILES[‘file’][‘tmp_name’], “uploads/$file_name”); }
6. $_COOKIE
- Contains HTTP cookies sent by the client to the server. You can retrieve the cookie values from this array.
- Example:
setcookie(“user”, “John Doe”, time() + 3600); // Sets a cookie echo $_COOKIE[‘user’]; // Outputs: John Doe
7. $_SESSION
- Stores session variables for individual users, allowing data persistence across pages.
- Example:
session_start(); $_SESSION[‘username’] = “JohnDoe”; echo $_SESSION[‘username’]; // Outputs: JohnDoe
8. $_REQUEST
- Contains the contents of $_GET, $_POST, and $_COOKIE. It can retrieve form or URL data regardless of the method used.
- Example:
echo $_REQUEST[‘name’]; // Retrieves “name” from either GET, POST, or COOKIE
9. $_ENV
- Contains environment variables. Commonly used to retrieve configuration settings.
- Example:
echo $_ENV[‘PATH’]; // Outputs the system PATH variable if set
10. $argc and $argv
- $argc: Provides the number of command-line arguments passed to the script.
- $argv: Contains an array of command-line arguments.
- Example:
// Command line: php script.php arg1 arg2 echo $argc; // Outputs: 3 (script name + 2 arguments) echo $argv[1]; // Outputs: arg1
Summary
Each superglobal array serves a specific function in handling data related to users, the server, cookies, sessions, and other environmental factors. When working with data from superglobal arrays, remember to validate and sanitize input to prevent security vulnerabilities, especially when working with $_GET, $_POST, and $_REQUEST.