What are some alternative methods to assigning variables based on user input in PHP?

When assigning variables based on user input in PHP, it's important to sanitize and validate the input to prevent security vulnerabilities like SQL injection or cross-site scripting attacks. One way to do this is by using filter_input() function with appropriate filters. Another method is to use the ternary operator to set default values if the user input is not valid.

// Using filter_input() function to sanitize and validate user input
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);

// Using ternary operator to set default values if user input is not valid
$age = isset($_POST['age']) ? intval($_POST['age']) : 0;