How can JavaScript code be integrated with PHP to enhance user interactions in a table display?

To enhance user interactions in a table display, JavaScript can be integrated with PHP to provide dynamic functionality such as sorting, filtering, and pagination. This can be achieved by using AJAX to communicate between the client-side JavaScript code and the server-side PHP code, allowing for real-time updates to the table display based on user actions.

<?php
// PHP code to fetch data from database and output as JSON
$data = array(
    array('id' => 1, 'name' => 'John', 'age' => 25),
    array('id' => 2, 'name' => 'Jane', 'age' => 30),
    array('id' => 3, 'name' => 'Alice', 'age' => 22)
);

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