What potential issue arises when using document.write within a while loop in PHP?

When using document.write within a while loop in PHP, the issue that arises is that the document.write function will overwrite the entire HTML document each time it is called, rather than appending to it. This can result in unexpected behavior and potentially break the structure of the webpage. To solve this issue, you can store the output in a variable during each iteration of the while loop and then echo the final result outside of the loop.

<?php
$output = "";
$i = 0;
while ($i < 5) {
    $output .= "Iteration $i <br>";
    $i++;
}
echo $output;
?>