Welcome to The Coding College! PHP provides several built-in superglobal variables, which are always accessible, regardless of scope. These superglobals are essential for handling form data, session management, and server-related information. This guide will help you understand PHP’s global variables and their practical use cases.
What Are Superglobals in PHP?
Superglobals are predefined variables in PHP that are always accessible, regardless of scope. You can use them in any function, class, or file without declaring them as global
.
List of PHP Superglobals
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
1. $GLOBALS
The $GLOBALS
array stores all global variables in your PHP script.
Example: Accessing Global Variables
<?php
$x = 10;
$y = 20;
function addNumbers() {
global $x, $y;
$GLOBALS['z'] = $x + $y;
}
addNumbers();
echo $z; // Output: 30
?>
2. $_SERVER
The $_SERVER
superglobal contains information about headers, paths, and script locations.
Example: Retrieving Server Information
<?php
echo $_SERVER['PHP_SELF']; // Current script name
echo $_SERVER['SERVER_NAME']; // Server name
echo $_SERVER['HTTP_HOST']; // Host name
echo $_SERVER['REMOTE_ADDR']; // Client IP address
echo $_SERVER['SCRIPT_NAME']; // Path of the current script
?>
3. $_REQUEST
The $_REQUEST
superglobal collects data from $_GET
, $_POST
, and $_COOKIE
.
Example: Handling Form Data
<form method="post" action="process.php">
Name: <input type="text" name="name">
<input type="submit">
</form>
<?php
// process.php
echo "Hello, " . $_REQUEST['name']; // Output: Hello, [user input]
?>
4. $_POST
The $_POST
superglobal is used to collect form data sent via the HTTP POST method.
Example: Processing POST Data
<form method="post" action="process.php">
Email: <input type="email" name="email">
<input type="submit">
</form>
<?php
// process.php
echo "Your email is: " . $_POST['email'];
?>
5. $_GET
The $_GET
superglobal is used to collect data sent via the URL (query string).
Example: Passing Data via URL
<a href="process.php?name=John&age=30">Click here</a>
<?php
// process.php
echo "Name: " . $_GET['name']; // Output: Name: John
echo "Age: " . $_GET['age']; // Output: Age: 30
?>
6. $_FILES
The $_FILES
superglobal is used to upload files.
Example: Handling File Uploads
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload">
<input type="submit" value="Upload">
</form>
<?php
// upload.php
if (isset($_FILES['fileToUpload'])) {
$fileName = $_FILES['fileToUpload']['name'];
$fileTmp = $_FILES['fileToUpload']['tmp_name'];
if (move_uploaded_file($fileTmp, "uploads/" . $fileName)) {
echo "File uploaded successfully!";
} else {
echo "File upload failed.";
}
}
?>
7. $_ENV
The $_ENV
superglobal stores environment variables.
Example: Accessing Environment Variables
<?php
echo getenv("PATH");
?>
8. $_COOKIE
The $_COOKIE
superglobal is used to retrieve data stored in cookies.
Example: Using Cookies
<?php
// Set a cookie
setcookie("user", "John Doe", time() + (86400 * 30), "/");
// Access the cookie
if (isset($_COOKIE["user"])) {
echo "User is " . $_COOKIE["user"]; // Output: User is John Doe
} else {
echo "Cookie is not set.";
}
?>
9. $_SESSION
The $_SESSION
superglobal is used to store data across multiple pages.
Example: Using Sessions
<?php
// Start the session
session_start();
// Store session data
$_SESSION["user"] = "John Doe";
// Access session data
echo $_SESSION["user"]; // Output: John Doe
?>
Best Practices
- Sanitize Inputs: Always validate and sanitize data from superglobals like
$_POST
and$_GET
to avoid security issues. - Limit Use of
$_REQUEST
: Use$_POST
or$_GET
explicitly instead of$_REQUEST
for better control. - Session Security: Use session-related superglobals securely by implementing session timeout and regeneration.
- File Uploads: Validate file types and sizes when working with
$_FILES
.
Conclusion
PHP superglobals are powerful tools that make it easy to handle data from various sources like forms, URLs, cookies, and sessions. By mastering superglobals, you can build dynamic and secure web applications.
For more PHP tutorials and coding tips, visit The Coding College. Stay curious, keep coding, and unlock your programming potential!