How can AJAX requests be utilized in PHP to update page content dynamically?

To update page content dynamically using AJAX requests in PHP, you can create a PHP script that fetches data from the server and returns it in a format such as JSON. Then, use JavaScript to make an AJAX request to this PHP script, retrieve the data, and update the page content accordingly.

<?php
// ajax_request.php

// Simulate fetching data from the server
$data = ['message' => 'This is dynamic content from the server'];

// Return data in JSON format
header('Content-Type: application/json');
echo json_encode($data);
?>