Ah, I see. That's a bit trickier.
All I can think of is to use output buffering, maybe something like:
<?php
ob_start();
// ...bunch of PHP code and HTML
?>
<div id="source_code">
[[place_holder]]
</div>
</body>
</html>
<?php
$html = ob_get_clean();
$html = str_replace('[[place_holder]]', htmlspecialchars($html), $html);
echo $html;
?>