Skip to content

PHP Tutorial Superglobals

$_POST

Lesson 147 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

$_POST holds the values from a form sent with method="post". Like $_GET, it is an array of strings (or nested arrays).

index.php
PHP
<?php
$_POST = ["username" => "isha", "remember" => "1"];

$username = $_POST["username"] ?? "";
$remember = isset($_POST["remember"]);

echo "$username, remember: " . ($remember ? "yes" : "no");
Output
OUTPUT
isha, remember: yes

When is $_POST filled?

PHP fills $_POST when the request body has the content type application/x-www-form-urlencoded or multipart/form-data. That covers normal HTML forms.

It is empty for requests that send JSON. For a JSON API, read the raw body:

index.php
PHP
<?php
$json = file_get_contents("php://input");
$data = json_decode($json, true);

See Working with JSON Data.

Check the method first

index.php
PHP
<?php
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
    http_response_code(405);
    exit("Method not allowed");
}

A page that changes data should accept only POST. A visitor who simply opens the address (a GET) gets a clear refusal.

Nested and array values

index.php
PHP
<?php
$_POST = [
    "user" => ["name" => "Isha", "email" => "i@example.com"],
    "skills" => ["php", "sql"],
];

echo $_POST["user"]["name"] . " knows " . implode(" and ", $_POST["skills"]);
Output
OUTPUT
Isha knows php and sql

Limits

PHP stops reading very large requests. Two php.ini settings control it:

  • post_max_size: the biggest allowed request body.
  • max_input_vars: how many fields PHP will accept (1000 by default).

If a huge form silently loses fields, check max_input_vars.

Do not trust it

Everything in $_POST is chosen by the sender. A hostile request can include extra fields, skip fields, or send arrays instead of strings. Read only the keys you need, and validate each one.

index.php
PHP
<?php
$_POST = ["name" => "Isha", "is_admin" => "1"];

// Pick only the fields you expect
$allowed = ["name", "email"];
$data = array_intersect_key($_POST, array_flip($allowed));

print_r($data);
Output
OUTPUT
Array
(
    [name] => Isha
)

The extra is_admin field never reaches your code. This avoids a bug called mass assignment, where a visitor sets fields you never meant to expose.

warning
Saving $_POST directly
INSERT or update with the whole $_POST array lets a visitor write any column. Pick the fields explicitly.