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

Asked

Viewed 1,380 times

10

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

Is this really good practice? Why?

  • 3

    So "good practices" are always hideous, they’ve seen repeated mantras without context. Never use good practice. Do the right thing in each context. If you don’t know "the right thing" you are missing in some way, even following a "good practice". Programming by coincidence is still a mistake. The advice I can give you is to always ask for a reasoned explanation of what the person is saying when they suggest you do something in programming (this goes for blogs and answers in Sopt :) ). Know "why?" is the most important part of programming and probably any area of knowledge.

  • 1

    It’s a Newbie coding style Recommendation. That’s Why it’s typically mentioned in Introductory Books. I’ve grown accustomed to removing since I spent hours and hours hunting for the extra space bug at the end of the file.

1 answer

17


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 whitespace will not appear at the end of the files, and you will still 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 added a blank space at the end of the parts generated by included files.

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

  1. If you omit the lock and escape a character, you will receive a syntax error: Parse error: syntax error.
  2. If you close the tag ?> and accidentally escape a character, you may receive a: Warning: Cannot Modify header information, This is because there will be a way out before the headers.
  • 1

    What I call a liberating response capable of clarifying enough.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.