设为首页收藏本站language→→ 语言切换

鸿鹄论坛

 找回密码
 论坛注册

QQ登录

先注册再绑定QQ

查看: 1789|回复: 1
收起左侧

PHP语言参考 - 基本语法

[复制链接]
发表于 2010-2-24 13:38:37 | 显示全部楼层 |阅读模式
<DIV class=chapter lang=zh-cn><DIV class=titlepage><DIV><DIV><H1><A name=language.basic-syntax></A>第 10 章 基本语法</H1></DIV></DIV></DIV><H2>目录</H2><DL><DT><U><FONT color=#000099>从 HTML 中分离</FONT></U><DT><U><FONT color=#000099>指令分隔符</FONT></U><DT><U><FONT color=#000099>注释</FONT></U></DT></DL><DIV class=sect1 lang=zh-cn><DIV class=titlepage><DIV><DIV><H1><A name=language.basic-syntax.phpmode></A>从 HTML 中分离</H1></DIV></DIV></DIV><>当 PHP 解析一个文件时,会寻找开始和结束标记,标记告诉 PHP 开始和停止解释其中的代码。此种方式的解析可以使 PHP 嵌入到各种不同的文档中,凡是在一对开始和结束标记之外的内容都会被 PHP 解析器忽略。大多数情况下 PHP 都是嵌入在 HTML 文档中的,如下例所示。 <DIV class=informalexample><DIV class=phpcode><CODE><SPAN class=html><FONT face=Courier><>This is going to be ignored.</P><BR><SPAN class=default><FONT color=#0000bb><?php </FONT></SPAN><SPAN class=keyword><FONT color=#007700>echo </FONT></SPAN><SPAN class=string><FONT color=#dd0000>'While this is going to be parsed.'</FONT></SPAN><SPAN class=keyword><FONT color=#007700>; </FONT></SPAN></FONT><FONT face=Courier><SPAN class=default><FONT color=#0000bb>?&gt;<BR></FONT></SPAN><>This will also be ignored.</P></FONT></SPAN><FONT face=Courier></FONT></CODE></DIV></DIV><><FONT face=Courier></FONT></P><>还可以用更高级的结构: <DIV class=example><A name=id2643038></A><><B>例 10.1. 高级分离术</B></P><DIV class=example-contents><DIV class=phpcode><CODE><SPAN class=html><FONT face=Courier><SPAN class=default><FONT color=#0000bb><?php<BR></FONT></SPAN><SPAN class=keyword><FONT color=#007700>if (</FONT></SPAN><SPAN class=default><FONT color=#0000bb>$expression</FONT></SPAN></FONT><SPAN class=keyword><FONT face=Courier color=#007700>) {<BR>    </FONT></SPAN><FONT face=Courier><SPAN class=default><FONT color=#0000bb>?&gt;<BR></FONT></SPAN>    <STRONG>This is true.</STRONG><BR>    </FONT><SPAN class=default><FONT face=Courier color=#0000bb><?php<BR></FONT></SPAN><SPAN class=keyword><FONT face=Courier color=#007700>} else {<BR>    </FONT></SPAN><FONT face=Courier><SPAN class=default><FONT color=#0000bb>?&gt;<BR></FONT></SPAN>    <STRONG>This is false.</STRONG><BR>    </FONT><SPAN class=default><FONT face=Courier color=#0000bb><?php<BR></FONT></SPAN><FONT face=Courier><SPAN class=keyword><FONT color=#007700>}<BR></FONT></SPAN><SPAN class=default><FONT color=#0000bb>?&gt;</FONT></SPAN></FONT></SPAN></CODE></DIV></DIV></DIV><BR class=example-break>上例可正常工作,因为当 PHP 碰到结束标记 ?&gt; 时,就简单地将其后的内容原样输出直到碰到下一个开始标记为止。当然,上面的例子很做作,但是对输出大块的文本而言,脱离 PHP 解析模式通常比将所有内容用 <A href="http://www.php.net/manual/zh/function.echo.php"><U><FONT color=#000099>echo()</FONT></U></A> 或者 <A href="http://www.php.net/manual/zh/function.print.php"><U><FONT color=#000099>print()</FONT></U></A> 输出更有效率。 <></P><>可以在 PHP 中使用四对不同的开始和结束标记。其中两种,<?php ?> 和<SCRIPT language=php></SCRIPT> 总是可用的。另两种是短标记和 <SPAN class=productname>ASP</SPAN>&#8482; 风格标记,可以在 <VAR>php.ini</VAR> 配置文件中打开或关闭。尽管有些人觉得短标记和 <SPAN class=productname>ASP</SPAN>&#8482; 风格标记很方便,但移植性较差,通常不推荐。 <BLOCKQUOTE><><B>注意: </B>此外注意如果将 PHP 嵌入到 XML 或 XHTML 中则需要使用 <?php ?>以保持符合标准。 </P></BLOCKQUOTE><></P><><DIV class=example><A name=id2643139></A><><B>例 10.2. PHP 开始和结束标记</B></P><DIV class=example-contents><DIV class=phpcode><CODE><SPAN class=html><FONT face=Courier>1.  <SPAN class=default><FONT color=#0000bb><?php </FONT></SPAN><SPAN class=keyword><FONT color=#007700>echo </FONT></SPAN><SPAN class=string><FONT color=#dd0000>'if you want to serve XHTML_or_XML documents, do like this'</FONT></SPAN><SPAN class=keyword><FONT color=#007700>; </FONT></SPAN></FONT><FONT face=Courier><FONT color=#0000bb><SPAN class=default>?&gt;<BR></SPAN><BR></FONT>2.  </FONT><FONT face=Courier><SPAN class=default><FONT color=#0000bb><SCRIPT language=php><BR></FONT></SPAN><SPAN class=keyword><FONT color=#007700>echo </FONT></SPAN></FONT><SPAN class=string><FONT face=Courier color=#dd0000>'some editors (like FrontPage) don\'t<BR>              like processing instructions'</FONT></SPAN><SPAN class=keyword><FONT face=Courier color=#007700>;<BR></FONT></SPAN><FONT face=Courier><FONT color=#0000bb><SPAN class=default></SCRIPT><BR></SPAN><BR></FONT>3.  <? echo 'this is the simplest, an SGML processing instruction'; ?><BR>    <?= expression ?>This is a shortcut for "<? echo expression ?>"<BR><BR>4.  <% echo 'You may optionally use ASP-style tags'; %><BR>    <%= $variable; # This is a shortcut for "<% echo . . ." %></FONT></SPAN><FONT face=Courier></FONT></CODE></DIV></DIV></DIV><BR class=example-break><><FONT face=Courier></FONT></P><>上例中的 1 和 2 总是可用的,其中 1 是最常用,并建议使用的。 </P><>短标记(上例 3)仅在通过 <VAR>php.ini</VAR> 配置文件中的指令 <A href="http://www.php.net/manual/zh/ini.core.php#ini.short-open-tag"><U><FONT color=#000099>short_open_tag</FONT></U></A> 打开后才可用,或者在 PHP 编译时加入了 <VAR>--enable-short-tags</VAR> 选项。 <BLOCKQUOTE><><B>注意: </B>如果用 PHP 3 还可以通过 <B>short_tags()</B> 函数激活使用短标记。<SPAN class=emphasis><EM><STRONG>此方法只适用于 PHP 3!</STRONG></EM></SPAN></P></BLOCKQUOTE><></P><><SPAN class=productname>ASP</SPAN>&#8482; 风格标记(上例 4)仅在通过 <VAR>php.ini</VAR> 配置文件中的指令 <A href="http://www.php.net/manual/zh/ini.core.php#ini.asp-tags"><U><FONT color=#000099>asp___tags</FONT></U></A> 打开后才可用。 <BLOCKQUOTE><><B>注意: </B>对 <SPAN class=productname>ASP</SPAN>&#8482; 风格标记的支持是 3.0.4 版添加的。 </P></BLOCKQUOTE><></P><BLOCKQUOTE><><B>注意: </B>在以下情况应避免使用短标记:开发需要发行的程序或者库,或者在用户不能控制的服务器上开发。因为目标服务器可能不支持短标记。为了代码的移植及发行,确保不要使用短标记。 </P></BLOCKQUOTE></DIV></DIV></FONT></FONT>
您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2024-3-29 20:44 , Processed in 0.059899 second(s), 9 queries , Redis On.  

  Powered by Discuz!

  © 2001-2024 HH010.COM

快速回复 返回顶部 返回列表