Under what circumstances can you not write PHP's closing tag '?>'? php_PHP tutorial

WBOY
Release: 2016-07-13 10:19:51
Original
1285 people have browsed it

Under what circumstances can you not write the PHP closing tag "?>" and not write php

In some PHP projects, we often see that the code in some PHP files only has a start tag and no end tag. So under what circumstances can we not write this end tag, and under what circumstances must we write it?

Let’s look at 2 examples first:

The following code works fine:

<&#63;php
  echo 123456;
Copy after login

The following code will report an error:

<&#63;php
  echo 123456;
  

abc

Copy after login

Cause analysis:
The former is pure php code, so you don’t need to write an end tag, and it is not recommended to write an end tag; the latter has html code in addition to php code, and you must write an end tag.

So why is it not recommended to write the closing tag in the former?
Because when you don’t write the php end tag, by default there is php code from the start tag onwards. If there is other code, an error will be reported. php can only run scripts in the php tag. All characters outside the script, including spaces, carriage returns, and tabs that you cannot see, will be responded to the client as output content, so there is Unexpected things may happen. For example, if the header function is used in a file, and this file also contains another file, and there are empty characters outside the PHP tag of the included file, then a header already sent error will be reported. When we look at the source code of some web pages, we see a lot of spaces and line breaks at the beginning, which is caused by this reason.

Solution suggestion:

Library files, or some class files, etc. It is not recommended to add end tags to files with only pure PHP code.

I believe that what is described in this article has certain reference value for everyone’s PHP program design.

Why sometimes php does not write the closing tag terminator?

After looking for some information, everyone’s summary of PHP closing tags is as follows: Benefits: If this is a program included by others, without this end character, many problems can be reduced, such as: header, setcookie, session_start and other actions. There cannot be output before. If you accidentally add invisible characters (extra spaces, newlines) after ? to damage the page display, a Header already sent error will be reported. If you don't write it, you won't have this problem. In addition, you can directly move the cursor to the end and continue programming. The Bad: In Dreamweaver's view mode, it's a mess. The official explanation about the PHP closing tag is: PHP closing tag? The PHP analyzer in PHP is optional. However, if a closing tag is used, any space inserted after the closing tag by a developer, user, or FTP application may cause redundant output, PHP errors, subsequent output that cannot be displayed, and a blank page. Therefore, all PHP files should omit the PHP closing tag and insert a comment to indicate that this is the bottom of the file and locate the file relative to the application. This will help you make sure that the file has ended and not been deleted. Inappropriate:

Why do some people’s PHP files not write PHP language end tags - Technical Q&A

If the file is not executed separately, that is to say, it must be included by other files, it is recommended not to write the end tag. It is not not recommended, but it has its own special meaning

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/871104.htmlTechArticleUnder what circumstances can we not write the PHP closing tag "?" and not write php. In some PHP projects we often You will see that some codes in PHP files only have start tags and no end tags...
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template