views:

43

answers:

3

Hi,

when a carriage return follows a closing php tag, php doesn't print it.

How can I change this?

Thanks a lot

+5  A: 

That's normal behavior, and cannot be changed : the newline after a closing ?> is always ignored.


Here's the reference, in the FAQ of the PHP manual : Hey, what happened to my newlines?
(quoting, emphasis mine)

<pre>
<?php echo "This should be the first line."; ?>
<?php echo "This should show up after the new line above."; ?>
</pre>

In PHP, the ending for a block of code is either "?>" or "?>\n" (where \n means a newline).
So in the example above, the echoed sentences will be on one line, because PHP omits the newlines after the block ending.
This means that you need to insert an extra newline after each block of PHP code to make it print out one newline.

Why does PHP do this?
Because when formatting normal HTML, this usually makes your life easier because you don't want that newline, but you'd have to create extremely long lines or otherwise make the raw page source unreadable to achieve that effect.


And here are a couple of interesting reads about this :

Pascal MARTIN
In fact, nowerday it advised to not use the closing tag at all (on the end of the files)
Frenck
Not quite true Frenck. It's advised not to use the closing tag in your application code. If you're using PHP as a template language it's impossible NOT to use the closing tag.
Alan Storm
That's correct Alan, kinda forgot to mention that...
Frenck
A: 

It's a default behavior of the language.

If you need the line break, you put a echo "\n" or echo "<br>" as the last line of the script.

GSto
A: 

This is intended behavior (see Escaping from HTML):

[…] when PHP hits the ?> closing tags, it simply starts outputting whatever it finds (except for an immediately following newline - see instruction separation ) […]

Gumbo