What are the best practices for echoing HTML code within PHP scripts to avoid errors?

When echoing HTML code within PHP scripts, it's important to properly escape any special characters to avoid syntax errors or security vulnerabilities. One common approach is to use the htmlspecialchars() function to encode special characters like <, >, and &. This ensures that the HTML code is displayed correctly and safely within the PHP script.

&lt;?php
$html_code = &quot;&lt;div&gt;Hello, World!&lt;/div&gt;&quot;;
echo htmlspecialchars($html_code);
?&gt;