How can AJAX be utilized in PHP to achieve real-time updates without the need for page refresh?

To achieve real-time updates without the need for page refresh, AJAX can be utilized in PHP by sending asynchronous requests to the server to fetch updated data and then updating the content on the page dynamically. This allows for a smoother user experience as the page can be updated without reloading the entire page.

<?php
// PHP code to handle AJAX request
if(isset($_POST['data'])){
    // Process the data received from the AJAX request
    $data = $_POST['data'];
    
    // Perform any necessary operations with the data
    
    // Return the updated data as a response
    echo json_encode($updatedData);
}
?>