Skip to content
PHP Logo

How can PHP be used to store formatted form inputs in variables?

When working with form inputs in PHP, you can use the $_POST superglobal to retrieve the values submitted by the user. To store formatted form inputs in variables, you can sanitize and validate the input data before assigning them to variables. This helps prevent security vulnerabilities and ensures that the data is in the correct format for further processing.

php
// Retrieve form inputs and sanitize them
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);

// Store the sanitized form inputs in variables
// Now $name, $email, and $message contain the formatted user inputs