I’ve been dealing with this for years but it’s so nit-picky that I never looked into it. But I have always wondered.
In a PHP template, if I run this:
<p>
<?php echo 'Between "?>" and "</p>" is one linebreak.'; ?>
</p>
<p>
<?php echo 'Between "?>" and "</p>" is one space, then one linebreak.'; ?>
</p>
<p>
<?php echo 'Between "?>" and "</p>" is two linebreaks.'; ?>
</p>
This is the output:
<p>
Between "?>" and "</p>" is one linebreak.</p>
<p>
Between "?>" and "</p>" is one space, then one linebreak.
</p>
<p>
Between "?>" and "</p>" is two linebreaks.
</p>
I don’t understand this behavior. Why is it ignoring a single line break as if it’s not there (like in the first example)?
The PHP interpreter ignores a line break if it comes immediately after a closing
?>. This is for compatibility with text editors that always add a line break to the last line of a file.This is briefly mentioned in the PHP manual here and here.
If there’s a space before the line break then it’s output as normal, it’s only if the line break is immediately after that it ignores it.