How can PHP be used to save and display user input on a website?

To save and display user input on a website using PHP, you can use HTML forms to collect user input, then use PHP to process and store the input in a database or file. To display the saved user input, retrieve the data from the database or file and echo it back onto the webpage.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $user_input = $_POST["user_input"];
    
    // Save user input to a file
    $file = fopen("user_input.txt", "a");
    fwrite($file, $user_input . "\n");
    fclose($file);
}

// Display saved user input
$file = fopen("user_input.txt", "r");
while (!feof($file)) {
    $line = fgets($file);
    echo $line . "<br>";
}
fclose($file);
?>