PHP Foundation Reinforcement 2--Syntax

Source: Internet
Author: User

In this section, when embedding HTML, PHP has four tags,, <?php ?> <script language=‘php‘> </script> <? ?> (need to turn on Short_open_tag), <% %> (need to turn on asp_tags), can be opened in php.ini, recommended to use <?php ?> . There are three comments in PHP that are//,#,/**/.

2.1 PHP Tags

When parsing a file, PHP will look for the start and end tags, i.e. <?php ?> , and, this tells PHP to start and stop parsing the code between the two. This parsing allows PHP to be embedded in a variety of different documents, and any part outside the start and end tags will be ignored by the PHP parser. If the file content is pure PHP code, it is best to remove the PHP end tag at the end of the file. This avoids having to accidentally add a space or line break after the PHP end tag, which causes PHP to start outputting these blanks, and there is no intention to output the script at this time.

<? PHP Echo "Hello World"; // ... more code Echo "Last Statement"; //

2.2 Separating from HTML

Anything outside 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.

Echo

This will run as expected, because when the PHP interpreter encounters the?> end tag, it simply outputs the content as it is (unless it immediately follows the newline) until the next opening tag is encountered, and the exception is in the middle of the conditional statement, at which point the PHP interpreter determines which output is determined by the condition and which skips. See the following example. Use conditional structure:

if ($expressiontrue):?>  iftrueelse:?>   Otherwise this'll showendif

In the example above, PHP skips a paragraph not reached by the conditional statement, even if the paragraph is outside the PHP start and end tags. Because the PHP interpreter skips the block of conditional statements directly when the condition is not reached, PHP ignores it based on the condition. To output large pieces of text, jumping out of PHP parsing mode is usually more efficient than passing text through the Echo or print output.

You can use four pairs of different start and end tags in PHP. Two of them, <?php ?> and <script language="php"> </script> are always available. The other two types are short and ASP style tags that can be turned on or off in the php.ini configuration file. Although some people find it convenient to have short marks and ASP style tags, they are poorly ported and are generally not recommended for use. Note: Furthermore, if you embed PHP in XML or XHTML, you need to use <?php ?> tags to keep up with the standards.

2.3 Instruction Delimiter

Like C or Perl, PHP needs to end the instruction with a semicolon after each statement. An end tag in a PHP code implies a semicolon; the last line in a PHP snippet can end without a semicolon. If there are new rows later, the end tag of the snippet contains the end of the line.

<? PHP     Echo "This is a test"; Echo Echo

Note: The end tag of the PHP snippet at the end of the file can be omitted, and in some cases it would be better to omit it when using include or require, so that unwanted whitespace characters do not appear at the end of the file and can still output the response headers. It is also handy when using output buffering, and you will not see unexpected whitespace characters generated by the include file.

2.4 Notes

PHP supports c,c++ and Unix Shell-style (Perl-style) annotations. For example:

<? PHP     Echo // This is a one-line C + + style comment    /* This was a multi line comment        */    Echo "This is yet another test";     Echo # This is a one-line shell-style comment

A single-line comment only comments to the end of the line or the current PHP code block, depending on which first appears. This means that in//...? > or # ...? The HTML code after > will be displayed:?> jump out of PHP mode and return to HTML mode,//or # does not affect this. If the asp_tags configuration option is enabled, the behavior is the same as//%> or #%>. However, the </script> tag does not jump out of PHP mode in a single-line comment.

# echo ' simple ';? > exampleheader above would say ' this was an  

C-style annotations end when they hit the first */. Make sure that you do not nest C-style annotations. This error is easily encountered when trying to comment out a chunk of code.

<? /*    */

PHP Foundation Reinforcement 2--Syntax

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.