What are PHP superglobals, and what are some of the most commonly used ones?
Short answer
Superglobals are built in PHP variables that are always accessible from any scope throughout a script, including inside functions and classes, without needing to explicitly declare them as global, and commonly used superglobals include $_GET and $_POST for accessing submitted…
Advertisement
AModel answer
Superglobals are built in PHP variables that are always accessible from any scope throughout a script, including inside functions and classes, without needing to explicitly declare them as global, and commonly used superglobals include $_GET and $_POST for accessing submitted form or URL data, $_SESSION for accessing session data, $_SERVER for accessing information about the current request and server environment, and $_FILES for accessing uploaded file information.
echo $_SERVER['REQUEST_METHOD'];
$username = $_POST['username'] ?? '';Real-world example
A login form reads the submitted username and password directly from the $_POST superglobal after the form is submitted, without needing to pass that data through any additional function parameters.
Likely follow-ups
- Why are superglobals accessible everywhere without needing to be declared global first?
- What security precautions should be taken when using data from superglobals like $_GET or $_POST?
Related topics