Home >Backend Development >PHP Tutorial >Introduction to PHP basic syntax (1)_PHP tutorial
Separated from HTML
PHP scripts are caused by a pair of special tags in the file. When the interpreter analyzes a file, all special tags except For ordinary files, the interpreter will not do any processing on them, while files within special marks will be analyzed and executed by the interpreter as PHP code. This mechanism will allow you to embed PHP code in HTML: the content outside the PHP tag will be completely separated and not processed, while the inner content will be parsed as PHP code.
There are four sets of tags used to mark PHP code blocks, only two of which are always valid ( and ); the other two sets can be turned on or off in the php.ini configuration file. Although short-form markup and ASP-style markup may be less labor-intensive, there may be situations where you must use long-form markup. For example, if you plan to embed PHP code in XML or XHTML, you will need to use tags of the form to comply with XML standards.
The tags supported by PHP are:
<span class="html"><font face="NSimsun">1. <span class="default"><?php </SPAN><SPAN class=keyword>echo(</SPAN><SPAN class=string>"if you want to serve XHTML or XML documents, do like thisn"</SPAN><SPAN class=keyword>); </SPAN></FONT><FONT face=NSimsun><SPAN class=default>?><br></span><br>2. <span class="default"><? </SPAN><SPAN class=keyword>echo (</SPAN><SPAN class=string>"this is the simplest, an SGML processing instructionn"</SPAN><SPAN class=keyword>); </SPAN></FONT><FONT face=NSimsun><SPAN class=default>?><br></span> <span class="default"><?= expression ?></span> This is a shortcut for "<span class="default"><? </SPAN><SPAN class=keyword>echo </SPAN><SPAN class=default>expression ?></span>"<br>3. </font><font face="NSimsun"><span class="default"><script language="php"><br> </span><span class="keyword">echo (</span></font><span class="string"><font face="NSimsun">"some editors (like FrontPage) don't<br> like processing instructions"</font></span><span class="keyword"><font face="NSimsun">);<br></font></span><font face="NSimsun"><span class="default"></script><br></span><br>4. <% echo ("You may optionally use ASP-style tags"); %><br><%= $variable; # This is a shortcut for "<% echo . . ." %></font></span> |
The preferred method is the first method , because it allows you to use XML structures such as Use PHP in XHTML code.
The second way is not always available. Only available if you activate the short_open_tag option in the php.ini configuration file. You can also activate short-form tags by using the short_tags() function (PHP 3 only), or by using the --enable-short-tags option to configure PHP. In the php.ini-dist configuration file, the short format tag is enabled by default, but it is recommended that you do not use this tag.
Enabling the option asp_tags in the PHP configuration file will activate the fourth method.
Note: ASP style markup was added in PHP 3.0.4
Note: When developing large applications or libraries for distribution , or when developing programs on a PHP server that is not under your control, please do not use short-form tags because the target server may not support short-form tags. To facilitate portability, please ensure that short-form tags are not used in code for redistribution.
PHP code block end tags automatically include the most recent ending newline (if one exists). Furthermore, the closing tag 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:
More advanced detachment
<?phpif ($expression) { ?> <strong>This is true.</strong> <?php} else { ?> <strong>This is false.</strong> <?php}?> |
PHP will directly output the closing tag and the next opening tag Any non-PHP code in . When a large amount of text needs to be output, exiting PHP parsing mode will be more efficient than using echo() or print() or similar functions to print all the text. 1