Have you ever noticed that PHP eats the newlines after a closing PHP tag?  Not sure what I mean?  There is lots on Google about it.  Here is an example.

Hello there!
<?php

// this is just a dump PHP block

?>
How are you?

becomes:

Hello there!
How are you?

I was talking about this with a coworker tonight.  He is trying to generate some XML and, like me and Chis Shiflett, is anal about his output.  You see, what happens in modern use of PHP as a template language is something like this:

<?php

$subelement = range(1, 10);

?>
<somexml>
    <element>
        <?php foreach($subelement as $e) { ?>
            <subelement><?php echo $e; ?></subelement>
        <?php } ?>
    </element>
</somexml>

That code will output this mess:

<somexml>
    <element>
                    <subelement>1</subelement>
                    <subelement>2</subelement>
                    <subelement>3</subelement>
                    <subelement>4</subelement>
                    <subelement>5</subelement>
                    <subelement>6</subelement>
                    <subelement>7</subelement>
                    <subelement>8</subelement>
                    <subelement>9</subelement>
                    <subelement>10</subelement>
            </element>
</somexml>

So, why does PHP do this?  Well, you have to go back 11 years.  PHP 3 was emerging.  I was just starting to use it for Phorum at the time.  There were two reasons.

The first was that you would want the newline after the first closing tag to be removed as it would remove the existence of the PHP block completely.  At the time, people were shunned for writing PHP as a tag looking language.  ColdFusion was new then too and the PHP community liked to point and laugh at it.

The second case (and this is probably a more legitimate one) was that many editors (some still do this for some insane reason) force every friggin file to end in a newline.  We did not have output buffering in those days.  It was the stone age man.  So, to get around the "Headers already sent" errors, Zeev decided to make the PHP ending tag be "?> with an optional newline".  It was a heated debate on the PHP Internals (then php-dev) list.  So much that I remembered it and dug it up on MARC.

Heck, now I want to add to it.  I would like it please if PHP could remove any leading, non-newline whitespace before an open tag.  That would solve this problem.  Yeah, more magic!  Nothing like it.

To me, the worst alternative to all this is the lack of a closing tag in a file.  My OCD just can't deal with that.  Please, baby seals cry when you don't use a closing tag.