


Under what circumstances can you not write PHP's closing tag '?>'? php_PHP tutorial
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:
<?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

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

Fibers was introduced in PHP8.1, improving concurrent processing capabilities. 1) Fibers is a lightweight concurrency model similar to coroutines. 2) They allow developers to manually control the execution flow of tasks and are suitable for handling I/O-intensive tasks. 3) Using Fibers can write more efficient and responsive code.

The PHP community provides rich resources and support to help developers grow. 1) Resources include official documentation, tutorials, blogs and open source projects such as Laravel and Symfony. 2) Support can be obtained through StackOverflow, Reddit and Slack channels. 3) Development trends can be learned by following RFC. 4) Integration into the community can be achieved through active participation, contribution to code and learning sharing.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function