What are the risks of outputting PHP code using echo in a PHP file and how can this be avoided when dealing with HTML code?

Outputting PHP code using echo in a PHP file can pose a security risk as it may inadvertently expose sensitive information or allow for code injection. To avoid this, it is recommended to separate PHP logic from HTML presentation by using PHP opening and closing tags within the HTML code.

<?php
// Example of separating PHP logic from HTML presentation
$variable = "Hello World!";
?>

<!DOCTYPE html>
<html>
<head>
    <title>PHP Output Example</title>
</head>
<body>
    <h1><?php echo $variable; ?></h1>
</body>
</html>