Under what circumstances can not write PHP end tag "?>"

Source: Internet
Author: User

We often see that some PHP files in the code is only the start tag, and there is no end tag, then under what circumstances can not write this end tag, and under what circumstances must write? Let's take a look at 2 examples:

The following code works correctly:

<? PHP     echo 123456;

The following code is an error:

<? PHP     echo 123456;     <p>abc</p>

Cause Analysis:

The former is pure PHP code, you can not write the end tag, and do not recommend to write the end tag, the latter in addition to PHP code, and HTML code, you must write the end tag.

Why is it not recommended that the former write the end tag?

When you do not write the PHP end tag, the default from the start tag is the PHP code, if there are other code, it will be an error. PHP can only run the script inside the PHP tag, all characters outside the script, including the space you can't see or the carriage return, the tab symbol, will be response to the client as the output, which may produce unexpected things. For example, the file is used in the header function, the file also contains another file, and the file contained in the PHP tag has null characters, this time will be reported to the header already send error. We look at some of the Web page source code to see the beginning of a lot of space and newline, that is because of this cause.

Solution Recommendations:

library files, or some class files, and so on only pure PHP code files are not recommended to add the end tag.

Under what circumstances can not write PHP end tag "?>"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.