Is it a good practice not to close the .php file with? after an XHR call made from an .html file?

11

Should I close the PHP <?php tag with ?> ? A friend with more experience advised me not to close saying it was a "good programming practice", I never understood why, but I follow the recommendation ever.

Is this really a good practice? Why?

    
asked by anonymous 09.09.2014 / 04:27

1 answer

16

PHP DOC

  

The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is useful when using include or require, so unwanted white space will not appear at the end of the files, and you still will be able to add headers to the response after. It is also useful if you use output buffering, and you do not want to have a blank space added to the end of the parts generated by included files.

When you omit the closing tag ?> it prevents white space or line breaks that may occur accidentally at the end of the file. For this reason many programmers choose not to close and you will find many frameworks that adopt this procedure.

  • If you omit closing and escape a character, you will get a syntax error: Parse error: syntax error .
  • If you close the ?> tag and accidentally escape a character, you may get a: Warning: Can not modify header information , because there will be an output before the headers.
  • 09.09.2014 / 04:45