How can PHP and JavaScript be effectively combined to achieve dynamic content updates while maintaining server-side functionality?

To achieve dynamic content updates while maintaining server-side functionality, PHP can be used to handle server-side processing and database interactions, while JavaScript can be used to dynamically update the content on the client-side without needing to reload the entire page.

<?php
// PHP code to retrieve data from the database
$data = fetchDataFromDatabase();

// Encode the data in JSON format to be used by JavaScript
$jsonData = json_encode($data);
?>

<script>
// JavaScript code to dynamically update content on the client-side
var jsonData = <?php echo $jsonData; ?>;

// Use the jsonData variable to update the content on the page
updateContent(jsonData);
</script>