The history of PHP eating newlines after the closing tag

Tue, Mar 3, 2009 09:51 PM
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.
10 comments
Gravatar for Herman Radtke

Herman Radtke Says:

I think it is beneficial to not have a closing tag inside an include file. With <?php at the very top and no closing tag I can guarantee that there is no significant whitespace.

Gravatar for Bruce Weirdan

Bruce Weirdan Says:

<blockquote>
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.
</blockquote>
They do that for the very valid reason of being able to concatenate text files later. Otherwise file ending with a line-comment (like // in php or -- in sql), while concatenated to another file, would make the first line of that file a line-comment too.

<blockquote>
Please, baby seals cry when you don't use a closing tag.
</blockquote>
You'd better fix your baby seals to deal with valid php syntax silently.

Gravatar for James Dempster

James Dempster Says:

I much prefer to see no close php tag in files that contain nothing but PHP.

I don't see the benefit of having a close tag that could cause unwanted white space in a php only file.

Gravatar for David

David Says:

Shouldn't your template look more like this:

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

That said, I often have issues when using php to output plain text since whitespace is a lot more relevant then. I found that adding a space after the closing php tag prevents php from eating the newline. But that probably won't help your OCD...

Gravatar for Brian Moon

Brian Moon Says:

@David: That just makes my source code hard to read. And no, the space trick won't help me. I have my editor trim all trailing whitespace in fact. It makes making patches for projects a PITA. But, makes me happy. I hate hitting my end key and getting put, 30 characters away from the actual end of the code.

@Herman: I knew that last line was going to get the most attention out of this whole blog post.

Gravatar for Giorgio Sironi

Giorgio Sironi Says:

Please note that the coding standards of Zend Framework, Drupal and Doctrine do not permit closing tags...

Gravatar for Jon

Jon Says:

While I understand your stand on this, but not having a closing tag is kind of like coding as a newbie HTML developer who only codes for IE.

Stop complaining and just write your code to work with this like the rest of the world. Who knows what will happen if they decide to "fix" this.

Gravatar for Brian Moon

Brian Moon Says:

@Jon: to whom are you speaking? It sounds like you like closing tags.

Comments are disabled for this post.