PHP Tag Parsing ____php

Source: Internet
Author: User
PHP Tags

When parsing a file, PHP looks for the start and end tags, which is <?php and?, which tells PHP to start and stop parsing the code between the two. This parsing allows PHP to be embedded in a variety of documents, and any part of the start and end tags will be ignored by the PHP parser.

PHP also allows the use of short tags. And, but not encouraged to use. Short tags can only be used if you activate the Short_open_tag configuration directive in php.ini or when you compile PHP using the configuration option--enable-short-tags.

If the file content is pure PHP code, it is best to delete the PHP end tag at the end of the file. This avoids the unexpected addition of spaces or newline characters after the PHP closing tag, which causes PHP to start outputting these blanks, and there is no intention of the output at this time in the script. <?php
echo "Hello World";

... more code

echo "last Statement";

The script ends here without the PHP closing tag separating from the HTML

Anything that goes beyond a pair of start and end tags is ignored by the PHP parser, which allows the PHP file to have mixed content. You can embed PHP in an HTML document, as shown in the following example. <p>this is going to being ignored by PHP and displayed by the browser.</p>
<?php Echo ' While this is going to be parsed. '; ?>
<p>this will also is ignored by PHP and displayed by the browser.</p> this would work as expected, because when the PHP interpreter encounters the?> closing tag, it simply The content is then output (unless immediately wrapped) until the next start tag is encountered, and the exception is in the middle of a conditional statement, at which point the PHP interpreter determines which output, which is skipped, based on the criteria. See the following example.

Use conditional structure:

Advanced separation of Example #1 using conditions <?php if ($expression = = true):?>
This'll show if the expression is true.
<?php Else:?>
Otherwise this'll show.
<?php endif?> In the example above, PHP skips paragraphs that are not reached by the conditional statement, even if the paragraph is outside the PHP start and end tags. Because the PHP interpreter skips the conditional statement block directly when the condition is not reached, PHP ignores it according to the criteria.

To output large pieces of text, it is usually more efficient to jump out of the PHP parsing mode than to pass the text through the Echo or print output.

You can use four pairs of different start and end tags in PHP. Two of these, <?php?> and <script language= "PHP" > </script> are always available. The other two are short tags and ASP style tags that can be turned on or off in the php.ini configuration file. Although some people feel that short tags and ASP style tags are convenient, they are less portable and are often deprecated.

Note:

Also note that if you embed PHP in XML or XHTML, you need to use the <?php?> tag to keep up with the standards.

Example #2 PHP start and end tag 1. <?php echo ' If you want to serve XHTML or XML documents, does it like this ';?>

2. <script language= "PHP" >
Echo ' Some editors (like FrontPage) don\ ' t
Like processing instructions ';
</script>

3.? Echo ' This is the simplest, a SGML processing instruction ';?>
<?= expression?> is a shortcut for ". echo Expression?> "

4. <% echo ' Optionally use asp-style tags '; %>
<%= $variable; # This is a shortcut for ' <% echo ... '%>

The tags used in 1 and 2 in the example above are always available, with example 1 being the most commonly used and recommended.

The short mark (Example 3) is only available after the instruction Short_open_tag in the php.ini configuration file is opened, or the--enable-short-tags option is added to PHP compilation.

The ASP style tag (example 4) is only available after the instruction Asp_tags is opened through the php.ini configuration file.

Note:

You should avoid using short tags in the following situations: Develop programs or libraries that need to be published again, or develop on a server that users cannot control. Because the target server may not support short tags. For code portability and distribution, make sure you don't use short tags.

Note:

In PHP 5.2 and prior versions, the interpreter does not allow the entire contents of a file to be a start tag <?php. This type of file is allowed from PHP 5.3, but there is one or more white spaces after the tag is started.

Note:

Since PHP 5.4, the short form of the echo tag <?= is always recognized and legal, regardless of the Short_open_tag settings.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.