Back to course

Introduction to Superglobals ($\_GET, $\_POST, $\_REQUEST)

PHP: The Complete 0 to Hero Bootcamp

27. Introduction to Superglobals ($_GET, $_POST, $_REQUEST)

Superglobals are built-in variables that are always accessible in all scopes (global, local, functions, etc.). They are crucial for handling web requests and user interaction.

1. $_GET

An associative array of variables passed to the current script via the URL parameters (the Query String). Data is visible in the URL and should only be used for non-sensitive data, searching, or navigation.

  • URL: page.php?item=shirt&color=blue

php

2. $_POST

An associative array of variables passed to the current script via the HTTP POST method (usually from an HTML form).

  • Key Feature: Data is sent in the body of the request, making it invisible in the URL. Used for submitting sensitive data (passwords, large amounts of text).

3. $_REQUEST

An associative array that, by default, contains the contents of $_GET, $_POST, and $_COOKIE. It's generally discouraged in production code because it can lead to ambiguity (e.g., if a variable exists in both GET and POST).

Best Practice: Always use the specific superglobal ($_GET or $_POST) that matches the method you expect, for clarity and security.