Beginner Fundamentals
PHP Superglobals
Superglobals are built-in arrays that PHP makes available everywhere in your code. They hold data about requests, the server, and the user session.
$_GET
Holds data sent through the URL query string.
<?php
// URL: page.php?id=5
echo $_GET["id"]; // 5
$_POST
Holds data submitted from a form using the POST method.
<?php
// From <form method="post">
echo $_POST["username"];
$_SERVER
Contains information about the server and the current request.
<?php
echo $_SERVER["REQUEST_METHOD"]; // GET or POST
echo $_SERVER["PHP_SELF"]; // current script path
$_SESSION
Stores data that persists across pages for one user. Start the session first.
<?php
session_start();
$_SESSION["user"] = "Bruno";
echo $_SESSION["user"]; // Bruno
Why they matter
$_GETand$_POSTreceive user input$_SERVERgives request details$_SESSIONremembers users between pages
Always treat user-supplied data as untrusted and validate it.