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.