Labs ICT
โญ Pro Login

Superglobals

PHP has a handful of special arrays that are always available no matter where you are in your code. They're called superglobals, and they carry important information about the request, the server, and the environment your script is running in. Think of them as the universe your PHP script lives in โ€” and these arrays tell you everything about that universe.

$_SERVER

$_SERVER is packed with information about the server and the current request. Want to know what HTTP method was used? Grab $_SERVER["REQUEST_METHOD"]. Need the user's IP address? $_SERVER["REMOTE_ADDR"] has it. What page did they come from? $_SERVER["HTTP_REFERER"] tells you.

<?php
echo "You used: " . $_SERVER["REQUEST_METHOD"];
echo "Your IP: " . $_SERVER["REMOTE_ADDR"];
echo "You came from: " . $_SERVER["HTTP_REFERER"];
?>
Try it Yourself โ†’

$_REQUEST

$_REQUEST is a combination of $_GET, $_POST, and $_COOKIE all rolled into one. It's convenient when you don't care how the data arrived โ€” you just want it. But it's also a bit sloppy. If both GET and POST have the same parameter name, you won't know which one you're reading. Use it for quick scripts, but be specific when you can.

<?php
$name = $_REQUEST["name"]; // could be from GET or POST
echo "Hello, " . $name;
?>
Try it Yourself โ†’

$_ENV and $GLOBALS

$_ENV holds environment variables โ€” things like database passwords or API keys that are set outside your PHP code. You'll see it more when you start using Docker or cloud hosting. $GLOBALS is a reference to all global variables in your script. It's useful when you need to access a global variable from inside a function without using the global keyword.

<?php
$x = 75;
function test() {
    echo $GLOBALS["x"]; // 75
}
test();
?>
Try it Yourself โ†’

๐Ÿงช Quick Quiz

Which superglobal holds form data sent via the POST method?