What are best practices for storing parsed values from input in PHP variables?

When storing parsed values from input in PHP variables, it is important to sanitize and validate the input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. Use PHP functions like htmlspecialchars() or filter_var() to sanitize the input before storing it in variables. Additionally, consider using prepared statements when interacting with databases to further protect against SQL injection.

// Example of storing parsed input values in PHP variables
$input = $_POST['user_input']; // Assuming input is received via POST method

// Sanitize the input using htmlspecialchars()
$sanitized_input = htmlspecialchars($input);

// Validate the input using filter_var()
if(filter_var($sanitized_input, FILTER_VALIDATE_EMAIL)) {
    $email = $sanitized_input;
} else {
    echo "Invalid email address";
}

// Store the sanitized and validated input in variables
$username = $sanitized_input;