Superglobals in PHP are predefined variables that are always accessible from any scope of a script. They provide valuable information about the server, client, and environment. Here are some of the most commonly used PHP superglobals:
// URL: http://example.com/?name=John&age=30
echo $_GET['name']; // Output: John
echo $_GET['age']; // Output: 30
echo $_POST['username'];
echo $_POST['password'];
echo $_REQUEST['name'];
session_start();
$_SESSION['username'] = 'John';
echo $_COOKIE['username'];
echo $_FILES['file']['name'];
echo $_FILES['file']['type'];
echo $_SERVER['SERVER_NAME'];
echo $_SERVER['REQUEST_METHOD'];
echo $_ENV['USER'];
echo $_ENV['HOME'];
Superglobals are extremely useful in PHP programming as they provide a convenient way to access and manipulate various aspects of the server, client, and environment. However, it’s essential to handle them with care, especially when dealing with user input, to prevent security vulnerabilities such as SQL injection and cross-site scripting (XSS) attacks.