How can the structure of the PHP script be improved to separate input processing, data retrieval, and HTML output for better readability and maintainability?

To improve the structure of the PHP script, we can separate input processing, data retrieval, and HTML output into distinct sections or functions. This separation of concerns makes the code more readable and maintainable by isolating different tasks. By organizing the code in this way, it becomes easier to understand and modify each part independently.

<?php

// Input processing
$input = $_POST['input'];

// Data retrieval
$data = fetchData($input);

// HTML output
echo "<html><head><title>Output</title></head><body>";
echo "<h1>Data Retrieved:</h1>";
echo "<p>$data</p>";
echo "</body></html>";

// Function to retrieve data
function fetchData($input) {
    // Data retrieval logic here
    return $data;
}

?>