Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP end tag "?>" [duplicate]

Tags:

php

tags

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 ?

like image 909
Ari Avatar asked Nov 13 '13 12:11

Ari


People also ask

Do I need to close <? PHP?

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.

How do I end a PHP tag?

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. <?

What is <? PHP tag?

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.

Do I need to end PHP file with ?>?

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.


1 Answers

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.

like image 96
Amal Murali Avatar answered Oct 14 '22 20:10

Amal Murali