PHP Superglobals
PHP superglobals are built-in global arrays in PHP that provide access to various types of data throughout your application. They are available in all scopes and can be used to access or manipulate data like form inputs, session variables, cookies, and more. Here’s a rundown of the main PHP superglobals with examples:
-
$_GETContains data sent via the URL query string (HTTP GET method).
// URL: example.com/index.php?name=John&age=25 echo $_GET['name']; // Outputs: John echo $_GET['age']; // Outputs: 25 -
$_POSTContains data sent via the HTTP POST method (usually from forms).
// Assume a form was submitted with a POST request echo $_POST['username']; // Outputs the value of the 'username' field echo $_POST['password']; // Outputs the value of the 'password' field -
$_REQUESTContains data from both
$_GETand$_POST, and also$_COOKIE.// Combining GET and POST data echo $_REQUEST['name']; // Outputs the 'name' value from GET or POST -
$_SERVERContains information about the server environment and HTTP headers.
echo $_SERVER['HTTP_USER_AGENT']; // Outputs the user's browser information echo $_SERVER['REQUEST_METHOD']; // Outputs the request method (GET, POST, etc.) -
$_SESSIONUsed to store session variables. Sessions must be started with
session_start()before using$_SESSION.session_start(); $_SESSION['username'] = 'JohnDoe'; echo $_SESSION['username']; // Outputs: JohnDoe -
$_COOKIEContains cookies sent by the client.
// Set a cookie first setcookie('user', 'JohnDoe', time() + 3600); // Expires in 1 hour echo $_COOKIE['user']; // Outputs: JohnDoe (if cookie is set) -
$_FILESContains information about files uploaded through a form.
// Assuming a form with file upload is submitted echo $_FILES['file']['name']; // Outputs the name of the uploaded file echo $_FILES['file']['size']; // Outputs the size of the uploaded file -
$_ENVContains environment variables.
echo $_ENV['PATH']; // Outputs the system path environment variable -
$_GLOBALSAn associative array containing all global variables.
$a = 10; function test() { global $a; echo $a; // Outputs: 10 } test();
These superglobals provide a way to access data across different parts of your PHP application, making it easier to handle form inputs, manage sessions, and interact with the server and environment.
Written by Shaikh Abdul Shahid
A Full-Stack Developer with experience in PHP, Laravel, JavaScript, and React.
Founder of Online Learner, sharing practical knowledge and programming tutorials.
Building and maintaining real-world web applications since 2017.
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
