How can JavaScript be used to print only a specific section of a page in PHP?

To print only a specific section of a page in PHP, you can use JavaScript to target that section and print it using the `window.print()` function. You can achieve this by creating a button or link that, when clicked, triggers the JavaScript function to print only the desired section.

<button onclick="printSection('section-to-print')">Print Section</button>

<script>
function printSection(sectionId) {
  var printContents = document.getElementById(sectionId).innerHTML;
  var originalContents = document.body.innerHTML;
  document.body.innerHTML = printContents;
  window.print();
  document.body.innerHTML = originalContents;
}
</script>