Home >Backend Development >PHP Problem >php>The difference between whether or not

php>The difference between whether or not

藏色散人
藏色散人Original
2021-11-29 10:02:351753browse

php > The difference between whether to close the tag or not: 1. If you close the tag, it will cause redundant output, php errors or blank pages, etc.; 2. If you do not close the tag, you can prevent harmful content after closing the tag; 3. If it is mixed use, you still need to write a closing tag.

php>The difference between whether or not

#The operating environment of this article: Windows7 system, PHP7.1, Dell G3.

php >What are the differences between yes and no? What are the benefits of not writing closing tags in PHP?

When I first started writing PHP code, I wanted to write PHP closing tags. In the past few years, my habit has changed and I no longer write PHP tags.

I first came into contact with this concept from Drupal. I was a little uncomfortable at first - why not write a closing tag? This feels very imperfect, and very uncomfortable for people with obsessive-compulsive disorder! Later, I encountered problems caused by closing tags at work, so I started not writing closed tags.

After reading some articles, the summary of PHP closing tags is as follows:

Benefits:

If this is a program included by others, without this terminator, it can be reduced There are many problems, such as: header, setcookie, session_start, these actions cannot have output before. If you accidentally add invisible characters (extra spaces, newlines) after ?> to damage the page display, "Header" will be reported. already sent" error. 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.

Disadvantages:

In dreamweaver's view mode, it's a mess.

The official explanation about the PHP closing tag is: The PHP closing tag "?>" is optional for the PHP analyzer in PHP. 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.

This is also mentioned in Zend’s code specification: http://framework.zend.com/manual/1.12/en/coding-standard.php-file-formatting.html

Code example:

Inappropriate:

The code is as follows:

<?php
echo "Here&#39;s my code!";
?>

Appropriate:

The code is as follows:

<?php
echo "Here&#39;s my code!";
/* End of file myfile.php */
/* Location: ./system/modules/mymodule/myfile.php */

Therefore , if it is a pure PHP code file, the ?> end identifier should be omitted.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of php>The difference between whether or not. For more information, please follow other related articles on the PHP Chinese website!

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

Related articles

See more