How can PHP be used to dynamically generate responses based on user input from a form?

To dynamically generate responses based on user input from a form in PHP, you can use the $_POST superglobal array to retrieve the user input data submitted through the form. Then, you can use conditional statements to determine the appropriate response based on the user input.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $userInput = $_POST["user_input"];

    if ($userInput == "hello") {
        echo "Hello, user!";
    } elseif ($userInput == "goodbye") {
        echo "Goodbye, user!";
    } else {
        echo "Invalid input. Please try again.";
    }
}
?>

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="user_input">
    <input type="submit" value="Submit">
</form>