What potential issues can arise when using JavaScript for displaying PHP output in real-time?

One potential issue that can arise when using JavaScript for displaying PHP output in real-time is that the PHP code may not be properly executed or displayed due to asynchronous behavior or timing issues. To solve this, you can use AJAX to make a request to a PHP file that generates the output, and then update the DOM with the response using JavaScript.

<?php
// PHP file (output.php) that generates the output
echo "Hello, World!";
?>
```

```javascript
// JavaScript code to make an AJAX request to output.php and update the DOM
let xhr = new XMLHttpRequest();
xhr.open('GET', 'output.php', true);
xhr.onload = function() {
  if (xhr.status >= 200 && xhr.status < 300) {
    document.getElementById('output').innerHTML = xhr.responseText;
  }
};
xhr.send();