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:
<?php echo 123456;
The following code will report an error:
<?php echo 123456;abc
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.
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:
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