What are some potential pitfalls to avoid when using Heredoc in PHP, especially in the context of generating dynamic code?

One potential pitfall when using Heredoc in PHP for generating dynamic code is accidentally including variables that are not properly sanitized, leading to security vulnerabilities like code injection. To avoid this, always make sure to properly escape and sanitize any user input before including it in the Heredoc block.

// Example of properly escaping and sanitizing user input before using Heredoc
$user_input = $_POST['user_input']; // Assuming this is user input
$escaped_input = htmlspecialchars($user_input); // Escape user input to prevent code injection

// Using Heredoc with sanitized input
$dynamic_code = <<<CODE
    <div>{$escaped_input}</div>
CODE;

echo $dynamic_code;