Should I close my PHP tags? [duplicate]
Possible Duplicate:
Why do some scripts omit the closing PHP tag, '?>'?
I've seen in a PHP framework (I can't remember which) that they didn't close the php
tag (?>
) at the bottom of the pages.
Why is that and shou开发者_运维技巧ld I do it too?
If it's a PHP file that contains no HTML, then don't close the tag.
This stops you from accidentally adding whitespace at the end of the file, therefore invoking browser output, and by extension headers, etc, which can cause a world of pain.
The framework you saw is most probably Zend Framework. From the code style section of their manual:
For files that contain only PHP code, the closing tag ("?>") is never permitted. It is not required by PHP, and omitting it´ prevents the accidental injection of trailing white space into the response.
Basically it means a PHP file won't have trailing whitespace.
If you include a file with trailing whitespace and try to set header() or cookies or stuff like that, then the trailing whitespace will cause a problem.
I personally prefer not to do it as it caused some unexpected troubles in the past. If you have white space(s) after closing tag, it might cause some troubles (like outputting this white space to browser, which is unpleasant if you are parsing XMLs) which are quite hard to debug.
You don't have to, as long as it's also the end of that script.
Adding ?>
to the end of a file doesn't serve any purpose, it just adds a few bytes to the filesize.
精彩评论