Home  >  Article  >  Backend Development  >  What are the benefits of not writing ?> at the end of the php file?

What are the benefits of not writing ?> at the end of the php file?

WBOY
WBOYOriginal
2016-07-25 08:57:331366browse
  1. //This is b.php
  2. include a.php
  3. session_start();
  4. ?>
Copy code

I have seen that it is not included in many php files before PHP's terminator, I saw a demo today, which just explained the benefits of not using the ?> terminator.

The following is a screenshot of a.php:

What are the benefits of not writing ?> at the end of the php file?

Pay attention to the fourth and fifth lines)

  1. include the file above.php
  2. session_start();
  3. ?>
Copy the code

Run the second file and you will find that it will throw a warning Say header already sent out

The main reason is that there are a few blank lines in a.php after the PHP terminator is called, so PHP will think it is html code. In this case, there will already be header output before session_start is called. Before calling session_start, there cannot be any output, so the program will throw a warning.

If you use ?>, you may make this mistake inadvertently. When the project is large and the amount of code is large, it is difficult to find such a problem. Therefore, it is best not to write ?> at the end of the PHP file. This is the officially recommended approach by zend.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn