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 ?
To recap, closing PHP tags (?>) are entirely optional if you're coding a PHP-only file and there's no intermingled HTML. Files containing single class definitions are good candidates. Omitting the closing tag has another benefit: it becomes impossible to accidentally add white space to the end of the file.
In PHP, statements are terminated by a semicolon (;) like C or Perl. The closing tag of a block of PHP code automatically implies a semicolon, there is no need to have a semicolon terminating the last line of a PHP block. <?
The <= tag is called short open tag in PHP. To use the short tags, one must have to enable it from settings in the PHP. ini file. First of all ensure that short tags are not disabled, To check it, go into php.
It's entirely optional but including it provides the opportunity to slip whitespace into the output by accident. If you do that in a file that you include or require before you try to output headers then you'll break your code.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With