How can a user input variable be properly defined and used in PHP code?

When defining a user input variable in PHP, it is important to sanitize and validate the input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. To properly define and use a user input variable in PHP, you can use the filter_input() function to retrieve and sanitize input from various sources such as GET, POST, or REQUEST.

$user_input = filter_input(INPUT_POST, 'user_input', FILTER_SANITIZE_STRING);
if($user_input){
    // Use the sanitized user input variable
    echo "User input: " . $user_input;
} else {
    echo "Invalid user input";
}