
PHP Predefined Variables (superglobals)
In PHP, there are several "superglobal" variables that are automatically created by the PHP runtime environment and can be accessed from any scope within a PHP script. These superglobals are:
- $_GET: an associative array of variables passed to the current script via the HTTP GET method.
- $_POST: an associative array of variables passed to the current script via the HTTP POST method.
- $_FILES: an associative array of items uploaded to the current script via the HTTP POST method.
- $_COOKIE: an associative array of variables passed to the current script via HTTP Cookies.
- $_SESSION: an associative array containing session variables available to the current script.
- $_REQUEST: an associative array that by default contains the contents of $_GET, $_POST, and $_COOKIE.
- $_SERVER: an associative array containing server and execution environment information such as headers, paths, and script locations.
- $_ENV: an associative array of variables passed to the current script via environment variables.
For example, to get the value of the name field in a HTML form submitted via GET method you can simply use:
$name = $_GET['name'];
You can also use these superglobals as an normal array, and also use array functions on them.
It is worth noting that, when possible, it is best practice to use the specific superglobal arrays (e.g. $_POST) instead of the $_REQUEST array because it makes the code more readable and can help prevent security issues such as cross-site scripting (XSS) attacks.