That's why the LedgerSMB project uses Template Toolkit with <?lsmb ?> tags (that and [% %] causes problems in LaTeX).
<html> <body> <?mypitag ... ?> </body> </html>
That's an SGML document. It doesn't matter where the PI tag is, or what is in it, it's an SGML document.
<?printstuff
'<html> <body> <p>fun stuff here</p> </body> </html>'
?>
That's still an SGML document.
<?php
# logic goes here
?>
That's an SGML document.
This is not splitting hairs. The reason is relatively subtle.... When you are doing SGML stuff an extra newline or a space in front of the opening tag really doesn't have any effect but if you do any output in binary form, it's a mess.
Let me give you an example.
Suppose I have a GUI application that prints some XML fragments to standard output. The most natural way to do this in PHP is to write an XML pre-processor which happens to make decisions based upon operations in the GUI. In other languages you would print to standard output explicitly and the GUI would be primary. In PHP the XML output would be primary and the GUI would be secondary.
In fact your entire GUI is sort of contained in XML fragments to start with.