Super Global i.e. (Global Variable) are built-in variables that are accessible from any part of a script without needing to explicitly declare them as global.
The PHP superglobal variables are:
- $_GET
- $_POST
- $_REQUEST
- $_SESSION
- $_COOKIE
- $_FILES
- $_SERVER
- $_ENV
- $GLOBALS
1.) $_GET
The $_GET superglobal is used to collect form data after submitting an HTML form with method=”get”. It can also be used to pass data via the URL (query string).
// URL: example.com?name=John&age=25
echo $_GET['name']; // Output: John
echo $_GET['age']; // Output: 25
2.) $_POST
The $_POST superglobal is used to collect form data after submitting a form using the POST method. It is commonly used for sensitive data as it does not display the data in the URL.
// Example of accessing form data submitted via POST
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
echo "Hello, " . $name;
}
3.) $_REQUEST
$_REQUEST is a combined collection of $_GET, $_POST, and $_COOKIE. It collects data from all three sources.
// Use $_REQUEST to get form data
$name = $_REQUEST['name']; // Gets data from GET, POST, or COOKIE
4.) $_SESSION
The $_SESSION superglobal is used to store session variables. Session variables are used to store user information that can be accessed across multiple pages during a session.
// Start session
session_start();
// Set session variables
$_SESSION['user'] = 'John Doe';
$_SESSION['role'] = 'admin';
// Access session variables
echo $_SESSION['user']; // Output: John Doe
5.) $_COOKIE
The $_COOKIE superglobal is used to access variables that are stored in cookies. Cookies are small files that store data on the user’s browser.
// Set a cookie
setcookie("user", "John Doe", time() + 3600, "/");
// Access the cookie
echo $_COOKIE['user']; // Output: John Doe (if cookie is set)
6.) $_FILES
The $_FILES superglobal is used to collect data about files uploaded through a form.
// HTML Form for file upload
// <form action="upload.php" method="POST" enctype="multipart/form-data">
// <input type="file" name="file">
// <input type="submit" value="Upload">
// </form>
// PHP to process the uploaded file
if ($_FILES['file']['error'] == UPLOAD_ERR_OK) {
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
}
7.) $_SERVER
The $_SERVER superglobal contains information about the server environment, such as headers, paths, and script locations.
// Accessing server details
echo $_SERVER['SERVER_NAME']; // Output: the name of the server
echo $_SERVER['REQUEST_METHOD']; // Output: GET or POST
echo $_SERVER['PHP_SELF']; // Output: the current PHP script name
Summary:
- $_GET: Collects data sent via the URL query string (GET method).
- $_POST: Collects data sent via the HTTP POST method (used for forms).
- $_REQUEST: A combination of $_GET, $_POST, and $_COOKIE.
- $_SESSION: Stores data that is preserved across pages during a session.
- $_COOKIE: Accesses data stored in cookies on the user’s machine.
- $_FILES: Accesses file data uploaded through a form.
- $_SERVER: Contains server and execution environment information.
- $_ENV: Contains environment variables.
- GLOBALS: Allows access to all global variables.
Advantages of Superglobals in PHP:
- They can be accessed from any part of the code without needing to declare them as global.
- They make it easy to handle user inputs, form submissions, and session data directly.
- They are designed for common web development tasks like retrieving server info, managing sessions, and processing forms.
- They reduce the need for complex data handling, making code simpler and easier to maintain.
- They provide an efficient way to access server details and environment settings.
Disadvantages of Superglobals in PHP:
- If not properly sanitized, superglobals (especially $_GET, $_POST, and $_REQUEST) can expose applications to security threats like SQL injection and XSS attacks.
- Overuse of $_REQUEST can make it unclear whether data came from a GET, POST, or COOKIE request, potentially causing confusion.
- Relying too heavily on superglobals for data access can lead to less modular and flexible code, making it harder to adapt in larger applications.
- Since superglobals are globally accessible, it’s easy to unintentionally modify them, which can introduce bugs, especially in large applications.
- Excessive or unnecessary use of superglobals might slightly impact performance, especially if data is fetched or processed repeatedly without need.