How can PHP be integrated with JavaScript to optimize the real-time display of data in a graph?

To optimize the real-time display of data in a graph, PHP can be integrated with JavaScript by using AJAX to fetch data from the server and update the graph dynamically without the need for a page refresh. This allows for a seamless and efficient display of real-time data.

<?php
// PHP code to fetch data from the server
$data = array(10, 20, 30, 40, 50);

// Convert PHP array to JSON format
$json_data = json_encode($data);

// Output JSON data
echo $json_data;
?>