What are the potential pitfalls of mixing PHP and HTML code in the same script, as seen in the provided example?
Mixing PHP and HTML code in the same script can make the code harder to read and maintain. It can also lead to errors if not properly handled, such as forgetting to close PHP tags or accidentally outputting HTML before headers are sent. To solve this issue, it's recommended to separate PHP logic from HTML presentation by using templates or frameworks like MVC.
<?php
// Separate PHP logic from HTML presentation
$variable = "Hello, World!";
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP and HTML Example</title>
</head>
<body>
<h1><?php echo $variable; ?></h1>
</body>
</html>