Each kind of form field arrives in PHP in a slightly different way.
Text, email, number, textarea
These arrive as strings, even when the type is number.
<?php
$_POST = ["name" => "Isha", "age" => "29", "bio" => "Loves PHP"];
$name = $_POST["name"] ?? "";
$age = $_POST["age"] ?? "";
var_dump($age);
string(2) "29"
Convert on purpose, after you validate.
Radio buttons
Only the selected option is sent. If nothing is chosen, the key is missing.
<?php
// <input type="radio" name="plan" value="basic"> ... value="pro">
$_POST = ["plan" => "pro"];
$plan = $_POST["plan"] ?? "basic";
echo "Plan: $plan";
Plan: pro
A single checkbox
An unchecked checkbox sends nothing. A checked one sends its value (or on if there is no value).
<?php
// <input type="checkbox" name="newsletter" value="yes">
$_POST = []; // the box was left unchecked
$subscribe = isset($_POST["newsletter"]);
var_dump($subscribe);
bool(false)
Use isset(), not a comparison with a value.
Several checkboxes
Add [] to the name so PHP collects them in an array.
<?php
// <input type="checkbox" name="topics[]" value="php"> ... value="css">
$_POST = ["topics" => ["php", "css"]];
$topics = $_POST["topics"] ?? [];
echo "Topics: " . implode(", ", $topics);
Topics: php, css
Select lists
A <select> sends the value of the chosen option.
<?php
$_POST = ["country" => "IN"];
echo "Country code: " . ($_POST["country"] ?? "");
Country code: IN
A <select multiple name="skills[]"> sends an array, like several checkboxes.
Check against allowed values
The visitor can send any value, not only the ones in your <select>. Compare with your own list.
<?php
$allowed = ["IN", "US", "UK"];
$country = "XX"; // someone edited the request
if (!in_array($country, $allowed, true)) {
echo "Please choose a valid country.";
}
Please choose a valid country.
Nested names
Names like user[name] and user[city] give you a nested array.
<?php
$_POST = ["user" => ["name" => "Isha", "city" => "Kochi"]];
echo $_POST["user"]["city"];
Kochi
A reading helper
A small function keeps the code tidy.
<?php
function post(string $key, string $default = ""): string
{
$value = $_POST[$key] ?? $default;
return is_string($value) ? trim($value) : $default;
}
$_POST = ["name" => " Isha "];
echo "[" . post("name") . "]";
[Isha]
The is_string() check matters: a visitor can send name[]=x, which makes $_POST["name"] an array and may break code that expects a string.
Plan for missing keys
?? or isset().