PHP Superglobals are built-in global arrays that are always accessible, regardless of scope. You can use them inside functions, classes, or files without declaring them as global.
PHP provides the following main superglobal variables:
$_GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
The $_GLOBALS array stores all global variables.
Contains server and execution environment information.
$_SERVER['PHP_SELF']
$_SERVER['SERVER_NAME']
$_SERVER['REQUEST_METHOD']
$_SERVER['HTTP_USER_AGENT']
$_SERVER['REMOTE_ADDR']
Collects data from GET, POST, and COOKIE.
⚠️ Not recommended for secure applications.
Collects form data sent using the GET method.
Collects form data sent using the POST method.
Used to handle file uploads via forms.
Contains environment variables.
Stores data on the client browser.
Stores session data on the server.
| Feature | GET | POST |
|---|---|---|
| Data visible | Yes | No |
| Data size | Limited | Unlimited |
| Security | Low | Higher |
| Use case | Search | Login, Forms |
Always validate user input
Sanitize data before output
Use filter_input() where possible
Avoid trusting user input blindly
Use prepared statements for database queries
Prefer $_POST over $_REQUEST
Escape output using htmlspecialchars()
Validate file uploads using $_FILES
Use sessions for sensitive data
PHP superglobals play a crucial role in handling user input, server data, sessions, and cookies. Understanding them is essential for building secure and dynamic PHP applications.
Take quizzes related to this topic and see where you stand!
Start Quiz Now