I've had an interesting phenomenon with a PHP end tag. I had a php file that was executed by an Ajax call. In the php file was included a php library file with assorted functions. When this library was included the php response included a bunch of blank lines. When I removed the end tag from the library this stopped happening. Can anyone explain to me what going on here ?
-
4You probably had some empty lines after the ?> tag, the empty lines are then interpreted as empty lines. When you remove the php end tag, the lines were seen as php code, and empty php lines aren't written to the output buffer. – Benz Nov 13 '13 at 12:13
-
3The library PHP file has a bunch of newline characters after its `?>`. Those get sent as output directly to the output buffer since they are not interpreted as PHP code inside ``. That's the most common cause of "headers already sent" errors, and the reason many frameworks omit the `?>`. – Michael Berkowski Nov 13 '13 at 12:13
-
1http://php.net/manual/en/language.basic-syntax.phptags.php "If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag" – Stephan Vierkant Nov 13 '13 at 12:14
-
I think that [this response](http://stackoverflow.com/a/4499749/1413922) in another similar question it's a perfect answer. – raulmarcosl Nov 13 '13 at 12:17
2 Answers
This is well documented. From the PHP Manual:
The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() or require(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.
Omitting the closing tag helps you prevent accidental whitespace or newlines from being added to the end of the file.

- 1
- 1

- 75,622
- 18
- 128
- 150
That's a core PHP feature: unlike other languages, you need to tag PHP code with a special tag (normally <?php
) because everything else is considered literal output:
This is not PHP
<?php
echo 'This is PHP' . PHP_EOL;
?>
This is not PHP either
D:\tmp>php test.php
This is not PHP
This is PHP
This is not PHP either
Although the manual mentions HTML, PHP doesn't really know/care what content-type is outside its tags.
If you forget to close a PHP block when further stuff follows you normally get a syntax error:
This is not PHP
<?php
echo 'This is PHP' . PHP_EOL;
This is not PHP either
D:\tmp>php test.php
PHP Parse error: syntax error, unexpected 'is' (T_STRING) in D:\tmp\borrame.php on line 6
Blank lines are a sort of special case because they are valid and almost invisible in almost all languages (PHP, HTML, CSS, JavaScript...) so they often unnoticed.
Once you've removed the ?>
tag, your literal blank lines have disappeared from the script output because they've become part of the PHP code (and, as such, they've started to get ignored).
Of course, blank lines are ignored by PHP but not necessarily by whatever you are generating which, as I said, does not need to be HTML: it can be a picture, a PDF document, an Excel spreadsheet. Bogus white lines can be easily avoided by not closing the last PHP block when it's the last part of the file.

- 142,137
- 41
- 261
- 360
-
today you should not use echo in your php anyways. Pointing to MVC, Template-Systems, Frameworks and so on. – Dwza Jul 01 '19 at 16:20