Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Variables and Constants

PHP $_POST superglobal

Reference for PHP $_POST: read form fields sent in a request body, checkboxes, array fields and JSON bodies, and validate and escape every value.

$_POST is an associative array of the fields sent in the body of an HTTP POST request when the content type is application/x-www-form-urlencoded or multipart/form-data. It is what an ordinary HTML form with method="post" produces.

$_POST stays empty for other body types. A request with Content-Type: application/json is not parsed into $_POST; read the raw body with file_get_contents("php://input") and decode it yourself. Like all request data, every value is a string (or an array of strings) chosen by the client and must be validated.

PHP
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $email = trim($_POST["email"] ?? "");
}

Try it

A command-line script has no request, so these examples fill $_POST by hand, exactly as PHP would after parsing a submitted form.

PHP Handling a simulated form submission

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Checkboxes, multiple fields and the JSON body

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
$_POST
$_POST["field"]
$_POST["field"][]
Part Description
$_POST Superglobal array. Empty on GET requests and for bodies that are not form-encoded.
Keys Field names from the name attributes of the form controls.
Values Strings, or nested arrays for name="items[]" and name="user[email]".

Type: array<string|int, string|array>

Usage notes

  • Check the method, not just the array. if ($_POST) is false for an empty form and for a GET. Test $_SERVER["REQUEST_METHOD"] === "POST" to decide whether a submission happened.
  • Unchecked checkboxes are absent. Use isset($_POST["agree"]). Unselected radio groups are also missing.
  • Missing keys warn. Read with ?? to avoid Undefined array key warnings.
  • Buttons. A submit button with a name is included only if it was the button used. Check it with isset($_POST["save"]) when a form has several buttons.
  • JSON APIs. Read php://input and use json_decode(). $_POST stays empty for application/json.
  • Validation is not optional. maxlength, required and type="email" in HTML are conveniences; a script can send anything. Validate on the server, then escape output with htmlspecialchars().
  • Protect state-changing forms with a CSRF token. See CSRF protection.
  • Limits. post_max_size (default 8M) and max_input_vars (default 1000) apply. If a body is larger than post_max_size, PHP leaves $_POST and $_FILES empty without an error message in your code.
  • File fields. Uploaded files are in $_FILES, not in $_POST; only the other text fields of a multipart form land here.
  • Post/Redirect/Get. After a successful submission, redirect with header() so a page refresh does not send the form again.
  • Prefer a filter for typed input. filter_input(INPUT_POST, ...) reads and validates in one step.