3

I have been working with Wordpress and other CMS and I have noticed that some of the php files do not have a closing ?>.

When I write my own files, I have to include the ?>, otherwise the software crashes. I'm wondering how they make it work, or if there are specific sections of a theme that accept this type of php files. Maybe the closing bracket ?> is embedded in a different file that I haven't seen.

Does anyone has an explanation for this?

If I decide to put the ?> on these files, will the system crash? or I can safely write them with the closing bracket?

rnevius
  • 26,578
  • 10
  • 58
  • 86
Sodanetworks
  • 105
  • 1
  • 9
  • 1
    it is now best practice to leave the closing `?>` tag off from php documents to avoid having extra white space after this tag that will intern cause `headers already sent errors` – cmorrissey Aug 20 '15 at 17:27
  • Does your software crash even when you omit closing tag in a pure php file? – Vasily Aug 20 '15 at 17:39
  • I thought my files crashed if I did not included the closing tag, but I have been doing it since then and they seem fine. – Sodanetworks Aug 21 '15 at 17:21

1 Answers1

17

From the official PHP documentation:

If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.

http://php.net/manual/en/language.basic-syntax.phptags.php

ryanpcmcquen
  • 6,285
  • 3
  • 24
  • 37
  • 1
    And that is the perfect and only answer, you'd do best to remember it and breathe a habit out of it, Will save you countless hours. – Blizz Aug 20 '15 at 17:30
  • 1
    Thank you to all who answered my question. Sorry about that this question was asked before. I searched but everyone has a different way to ask the same question, so my keywords didn't show any good answers. – Sodanetworks Aug 20 '15 at 22:58