[PHP series tutorial] (2) -- Basic PHP syntax

Source: Internet
Author: User
Document directory
  • 1. Separating from HTML
  • Ii. Command Separator
  • 3. Notes

To gain an in-depth understanding of PHP and make it easier to use PHP, This section describes the basic syntax of PHP.

1. Separating from HTML

PHP scripts are caused by a pair of special tags in a file. When the interpreter analyzes a file, the interpreter does not process all common files that are not marked, the file in the special mark will be analyzed and executed as the PHP code interpreter. This running mechanism will allow you to embed PHP code in HTML: content outside the PHP tag will be completely opened independently without any processing, and the internal content will be parsed as PHP code.

There are four sets of tags used to mark PHP code blocks, of which only two are always valid (<? PHP...?> And <script language = "php">... </SCRIPT>). The other two sets can be enabled or disabled in the PHP. ini configuration file. Although short-format and Asp-style tags may be effort-saving, you may have to use long-format tags in some cases. For example, if you want to embed PHP code in XML or XHTML, you will need to use <? PHP...?> To adapt to XML standards.

PHP supports the following markup:

Code 1: how to separate from HTML:

1. <? PHP echo ("if you want to serve XHTML or XML documents, do like this/N");?> </P> <p> 2. <? Echo ("this is the simplest, an SGML Processing Instruction/N");?> <Br/> <? = Expression?> This is a temporary cut for "<? Echo expression?> "</P> <p> 3. <MCE: script language =" php "> <! -- <Br/> echo ("some editors (like FrontPage) Don't <br/> like processing instructions "); </P> <p> // --> </MCE: SCRIPT> </P> <p> 4. <% echo ("You may optionally use ASP-style tags"); %> <br/> <% = $ variable; # This is a temporary cut for "<% echo... "%>

The first method is preferred. <? PHP...?>, Because it allows you to use PHP in XML structures, such as XHTML code.
The second method is not always available. Only when you activate the short_open_tag option in the PHP. ini configuration file is available. You can also use the short_tags () function (for PHP 3 only), or use the -- enable-short-tags option configure PHP to activate the short format tag. In the php. ini-Dist configuration file, the short format tag is enabled by default, but we recommend that you do not use this tag.

Enabling asp_tags in the PHP configuration file will activate the fourth method.
Note: Asp-style labels are added to PhP 3.0.4.
Note: Do not use short format tags when developing large applications or function libraries for distribution, or developing programs on a PHP server that is not under your control, because the target server may not support short format tags. For ease of transplantation, make sure that the short format mark is not used in the Code for redistribution.
The PHP code block end flag prize automatically contains the last ending new line (if any ). In addition, the end mark automatically implies a semicolon; you do not need to append a semicolon to the last line of the PHP code block.
PHP allows you to use the following structure:

Code 2: more advanced separation

<? Php <br/> if ($ expression) {<br/>?> <Br/> <strong> This is true. </strong> <br/> <? Php <br/>} else {<br/>?> <Br/> <strong> This is false. </strong> <br/> <? Php <br/>}< br/>?>

PHP will directly output the end tag and any non-PHP code in the next start tag. When a large amount of text needs to be output, exiting the PHP parsing mode will be more effective than printing all the text using echo (), print (), or such functions.

Ii. Command Separator

Command separation is similar to that of C or Perl-each statement is separated by a semicolon.
End mark (?>) The following code is equivalent to the end of an implicit statement:

<? Php <br/> echo "this is a test"; <br/>?> <Br/> <? PHP echo "this is a test"?>

3. Notes

PHP supports 'C', 'c ++ ', and Unix shell-style annotations. For example:

<? Php <br/> echo "this is a test "; // This is a one-line C ++ style comment <br/>/* this is a multi line comment <br/> yet another line of comment */<br/> echo "this is yet another test "; <br/> echo "one final test"; # This is Shell-Style comment <br/>?>

A single line comment is only annotated to the end of the line or the current PHP code block, depending on which one appears first.

<H1> This Is An <? PHP # echo "simple";?> Example. </p> <br/> <p> the header above will say 'This is an example '.

Be careful not to nest 'C'-style comments. This error may be made when many codes are commented.

<? Php <br/>/* <br/> echo "this is a test "; /* This comment will cause a problem */<br/>?>

A single line comment is only annotated to the end of the line or the current PHP code block, depending on which one appears first. This means //?> The following HTML code will be printed:?> Out of the PHP mode and returns the HTML mode, and // annotator does not affect the mode conversion.

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.