What are some best practices for creating a PHP script that generates results based on user input?

When creating a PHP script that generates results based on user input, it is important to validate and sanitize the input to prevent security vulnerabilities such as SQL injection attacks. Additionally, use proper error handling to provide informative messages to the user in case of invalid input. Lastly, structure your code in a modular and organized way to improve readability and maintainability.

<?php

// Validate and sanitize user input
$user_input = isset($_POST['user_input']) ? htmlspecialchars($_POST['user_input']) : '';

// Check if input is not empty
if (!empty($user_input)) {
    // Process user input and generate results
    $results = processUserInput($user_input);
    
    // Display results to the user
    echo "Results: " . $results;
} else {
    // Display error message if input is empty
    echo "Please enter valid input.";
}

function processUserInput($input) {
    // Add your processing logic here
    return $input;
}

?>