Reputation: 1059
I understand that you can omit PHP closing tags for files that are pure PHP, and that omitting the closing tag is considered good practice to avoid injecting extraneous whitespace in the output. The opinions/standards I have looked at thus far have made it clear that we should omit closing tags on pure PHP files, but it is much less clear if we should put the closing tags when files mix HTML and PHP. Some state outright that we need the closing tags in files mixing HTML and PHP without any evidence as to why, and others leave it ambiguous by only addressing the case of pure PHP files.
Is there some dangerous side effect to be aware of if a code base omits the PHP closing tag from all files, even if they contain HTML?
Upvotes: 0
Views: 84
Reputation: 781633
To answer the question in your title: the reason is because they don't know any better. They think of <?php
and ?>
as matching brackets, like {
and }
, and just make sure they're balanced out of habit.
There's no technical reason why it would ever be necessary.
Upvotes: 0
Reputation: 943940
Is there some dangerous side effect to be aware of if a code base omits the PHP closing tag from all files, even if they contain HTML?
No.
You need ?>
to switch to output mode if you want to write raw HTML.
You never need ?>
at the end of the entire file.
Upvotes: 6