What are best practices for displaying output from a PHP form?

When displaying output from a PHP form, it is important to properly sanitize and validate user input to prevent security vulnerabilities such as cross-site scripting (XSS) attacks. Additionally, formatting the output in a clear and organized manner can improve user experience. Using PHP functions like htmlspecialchars() to escape HTML characters and trim() to remove unnecessary whitespace can help ensure that the output is safe and well-formatted.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $output = trim($_POST["output"]);
    $output = htmlspecialchars($output);

    echo "<div>" . $output . "</div>";
}
?>