How can PHP be integrated into JavaScript files for dynamic content retrieval?

To integrate PHP into JavaScript files for dynamic content retrieval, you can use AJAX to make a request to a PHP file that fetches the data from the server and returns it to the JavaScript code. This allows you to dynamically update content on your webpage without needing to reload the entire page.

<?php
// data.php - PHP file to fetch data from the server
$data = array("name" => "John", "age" => 30);
echo json_encode($data);
?>
```

```javascript
// script.js - JavaScript file to retrieve data using AJAX
var xhr = new XMLHttpRequest();
xhr.open('GET', 'data.php', true);
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4 && xhr.status === 200) {
    var data = JSON.parse(xhr.responseText);
    console.log(data);
  }
};
xhr.send();