PHP Stripping Newlines
If you're picky about the format of your HTML like me, you've most likely noticed that PHP strips newlines that exist immediately after a closing PHP tag. Try the following code:
<table>
<tr>
<td>
<?php echo 'TEST'; ?>
</td>
</tr>
</table>
You might be surprised to see that this outputs the following:
<table>
<tr>
<td>
TEST </td>
</tr>
</table>
As PHP developers, most of us expect that anything not within a PHP block is left alone. After all, that's supposed to be the point of the opening and closing PHP tags. (This is the only exception of which I'm aware.) The expected output is as follows:
<table>
<tr>
<td>
TEST
</td>
</tr>
</table>
This has been mentioned on the NYPHP mailing list, but I don't remember seeing it discussed anywhere else, and no one could come up with a good reason for the behavior.
There is an explanation offered here in the manual:
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.
If you're like me, you're probably a bit surprised to see that this annoying behavior is supposedly there to "make your life easier." I can't think of a situation where I would want this behavior. Am I missing something obvious?





24 Comments
1.
Martel said:
2.
Örjan said:
3.
Rob Allen said:
4.
Chris Shiflett said:
5.
Stan said:
6.
Nico Edtinger said:
7.
Paul Gregg said:
8.
Örjan said:
9.
jperkins said:
10.
Paul Gregg said:
11.
JW said:
12.
Paul F. De La Cruz said:
13.
Mike Winger said:
14.
JL said:
15.
Peter Odding said:
16.
Joel Davis said:
17.
Geert De Deckere said:
18.
Marco said:
19.
Dominik said:
20.
Vin said:
21.
Beniji said:
22.
Buke Beyond said:
23.
Sam Souder said:
24.
Jens Roland said: