Superglobals · Lesson 2 of 8
$_POST
Read submitted form fields from the POST superglobal.
Lesson
$_POST contains parsed form data for a POST request, but values can be absent or have an unexpected shape. Check with ??, verify scalar types, validate allowed values, and enforce CSRF protection and authorization before a state-changing action.
An HTML form value should not be used directly in SQL or rendered without contextual escaping.
Further guidance
Superglobal values come from the request or process environment and can be missing or have an unexpected type. Check the key and shape before use, validate it for the operation, and avoid printing secrets or trusting client-controlled headers. Prefer the specific input source over merged request data.
Practice task: Create a fresh example for $_POST. Predict the result before running it, then change one input and explain why the result changes. Test a normal case and one boundary or invalid case where it applies. Compare your output with the example and note one rule you would use in a real project.
Code examples
$_POST
PHP
$quantity = filter_input(INPUT_POST, 'quantity', FILTER_VALIDATE_INT);
if ($quantity === false || $quantity === null || $quantity < 1) {
echo 'Quantity must be a positive integer.';
}
Expected output
A validation message when quantity is missing or invalid
Key points
- Read submitted form fields from the POST superglobal.
- Treat request and environment values as untrusted input.
- Check the expected type and presence before using a value.
Your learning progress
Finish this lesson
Optional practice
PHP playground
This site does not execute learner-provided PHP code. Connect a secured PHP sandbox service before enabling code execution.
Execution is disabled. No code is sent to a server.