What are the best practices for structuring a PHP page to avoid issues with dynamic content loading through JavaScript interactions?

When structuring a PHP page to avoid issues with dynamic content loading through JavaScript interactions, it is important to separate the PHP logic from the HTML presentation. One way to achieve this is by using AJAX to fetch data from the server asynchronously and update the page content dynamically. This ensures that the PHP code is only responsible for processing data and the JavaScript code handles the presentation.

<?php
// PHP logic to process data
$data = fetchData();

// Output JSON data for JavaScript to consume
echo json_encode($data);
?>