Hi,
when a carriage return follows a closing php tag, php doesn't print it.
How can I change this?
Thanks a lot
Hi,
when a carriage return follows a closing php tag, php doesn't print it.
How can I change this?
Thanks a lot
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 :
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.
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 ) […]