When parsing a PHP file, PHP looks for start and end tags, also known as , which tells PHP to start and stop parsing the code in between. This parsing method allows PHP to be embedded in various documents, and any part other than the opening and closing tags will be ignored by the PHP parser.
PHP also allows the use of short tags 和 ?>. This tag method can also be executed normally, but its use is discouraged here. Short tags can only be used by activating the short_open_tag configuration directive in php.ini or by using the configuration option --enable-short-tags when compiling PHP.
If the file content is pure PHP code, it is best to remove the PHP closing tag at the end of the file. This prevents PHP from accidentally adding spaces or newlines after the closing tag, which will cause PHP to start outputting these spaces when there is no intention to output them in the script.
test.php
<?php echo "Hello world"; // 脚本至此结束,并无 PHP 结束标记
Separated from HTML
Any content outside a pair of opening and closing tags will be ignored by the PHP parser, which allows PHP files to have mixed content. You can embed PHP into an HTML document, as shown in the example below.
This is going to be ignored by PHP and displayed by the browser.
This will also be ignored by PHP and displayed by the browser. will run as expected because When the PHP interpreter encounters the ?> end tag, it simply outputs the following content as it is (unless it is immediately followed by a newline - see command separator) until it encounters the next start tag; the exception is in the middle of a conditional statement, at which time The PHP interpreter will decide which output to output and which to skip based on conditional judgment. See example below.
This will show if the expression is true.
Otherwise this will show.
Example above PHP will skip paragraphs where the conditional statement is not fulfilled, even if the paragraph is outside the PHP opening and closing tags. Since the PHP interpreter will directly skip the conditional statement block if the condition is not met, PHP will ignore it based on the condition.
and are always available. The other two are short tags and ASP style tags, which can be turned on or off in the php.ini configuration file. Although some people find short tags and ASP-style tags convenient, they are less portable and generally not recommended.
Note:
Also note that if you embed PHP into XML or XHTML you need to use the tag to maintain standards compliance.