What is the issue with using PHP and Javascript document.write together?

When using PHP and JavaScript document.write together, the issue arises because document.write overwrites the entire document content when called after the page has finished loading. To solve this issue, you can use JavaScript's document.createElement and appendChild methods to dynamically add content to the page without overwriting existing content.

<?php
echo '<script>';
echo 'var newContent = document.createElement("div");';
echo 'newContent.innerHTML = "This is the new content added using JavaScript";';
echo 'document.body.appendChild(newContent);';
echo '</script>';
?>