What are the potential pitfalls of mixing PHP and HTML code within a single document, as seen in the provided code snippets?

Mixing PHP and HTML code within a single document can make the code harder to read and maintain. It can also lead to errors if not done properly, such as missing closing PHP tags or improperly nested HTML and PHP code. To solve this issue, it's recommended to separate PHP logic from HTML presentation by using PHP to generate the HTML code instead.

<?php
// Separate PHP logic from HTML presentation
$variable = "Hello, World!";
?>
<!DOCTYPE html>
<html>
<head>
    <title>PHP and HTML separation</title>
</head>
<body>
    <h1><?php echo $variable; ?></h1>
</body>
</html>