下面介绍PHP程序设计语言中的编程语句。
1 简单的语句
每行至多包含一条语句,例如:
以下为引用的内容:
<ol class="dp-c"> <li class="alt"><span><span class="vars">$argv</span><span>++; </span><span class="comment">// 正确的 </span><span> </span></span></li> <li> <span class="vars">$argc</span><span>--; </span><span class="comment">// 正确的 </span><span> </span> </li> <li class="alt"> <span class="vars">$argv</span><span>++; </span><span class="vars">$argc</span><span>--; </span><span class="comment">// 错误的</span><span> </span> </li> </ol>
2 复合语句
复合语句是包含在大括号中的语句序列,形如"{ 语句 }"。例如下面各段。
3 返回语句
一个带返回值的return语句不使用小括号"()",除非它们以某种方式使返回值更为显见。例如:
以下为引用的内容:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">return</span><span>; </span></span></li> <li> <span class="keyword">return</span><span> myDisk.size(); </span> </li> <li class="alt"> <span class="keyword">return</span><span> (</span><span class="vars">$size</span><span> ? </span><span class="vars">$size</span><span> : </span><span class="vars">$defaultSize</span><span>); </span> </li> </ol>
4 if与else语句
if-else语句应该具有如下格式:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">if</span><span> (condition){ </span><span class="comment">/* 进行操作的条件 */</span><span> </span></span></li> <li><span>statements; </span></li> <li class="alt"><span>} </span></li> <li> <span class="keyword">if</span><span> (condition) {</span><span class="comment">/*进行操作的条件. */</span><span> </span> </li> <li class="alt"><span>statements; </span></li> <li> <span>} </span><span class="keyword">else</span><span> {</span><span class="comment">/*进行操作的条件*/</span><span> </span> </li> <li class="alt"><span>statements; </span></li> <li><span>} </span></li> <li class="alt"> <span class="keyword">if</span><span> (condition) {</span><span class="comment">/*进行操作的条件*/</span><span> </span> </li> <li><span>statements; </span></li> <li class="alt"> <span>} </span><span class="keyword">else</span><span> </span><span class="keyword">if</span><span> (condition) {</span><span class="comment">/*进行操作的条件 */</span><span> </span> </li> <li><span>statements; </span></li> <li class="alt"> <span>} </span><span class="keyword">else</span><span>{</span><span class="comment">/*进行操作的条件*/</span><span> </span> </li> <li><span>statements; </span></li> <li class="alt"><span>} </span></li> </ol>
注意:if语句总是用"{"和"}"括起来,避免使用如下容易引起错误的格式:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">if</span><span> (condition) </span><span class="comment">//避免这种写法,他忽略了“{}” </span><span> </span></span></li> <li><span>statement; </span></li> </ol>
注释格式也可以像下面的这种方式写
以下为引用的内容:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">if</span><span> (condition) { </span></span></li> <li> <span class="comment">/*进行操作的条件*/</span><span> </span> </li> <li class="alt"><span>statements; </span></li> <li> <span>} </span><span class="keyword">else</span><span> { </span> </li> <li class="alt"> <span class="comment">/*进行操作的条件*/</span><span> </span> </li> <li><span>statements; </span></li> <li class="alt"><span>} </span></li> </ol>
只要可以描述清楚各分支之间的关系,在哪里写注释均可
5 for语句
一个for语句应该具有如下格式:
以下为引用的内容:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">for</span><span> (initialization; condition; update) { </span></span></li> <li><span>statements; </span></li> <li class="alt"><span>} </span></li> </ol>
一个空的for语句(所有工作都在初始化,条件判断,更新子句中完成)应该具有如下格式:
<ol class="dp-c"><li class="alt"><span><span class="keyword">for</span><span> (initialization; condition; update); </span></span></li></ol>
当在for语句的初始化或更新子句中使用逗号时,避免因使用三个以上变量,而导致复杂度提高。若需要,可以在for循环之前(为初始化子句)或for循环末尾(为更新子句)使用单独的语句。
6 while语句
一个while语句应该具有如下格式
以下为引用的内容:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">while</span><span> (condition) { </span></span></li> <li><span>statements; </span></li> <li class="alt"><span>} </span></li> </ol>
一个空的while语句应该具有如下格式:
<ol class="dp-c"><li class="alt"><span><span class="keyword">while</span><span> (condition); </span></span></li></ol>
7 do...while语句
一个do-while语句应该具有如下格式:
以下为引用的内容:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">do</span><span> { </span></span></li> <li><span>statements; </span></li> <li class="alt"> <span>} </span><span class="keyword">while</span><span> (condition); </span> </li> </ol>
8 switch语句
一个switch语句应该具有如下格式:
以下为引用的内容:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">switch</span><span> (condition) { </span></span></li> <li> <span></span><span class="keyword">case</span><span> ABC: </span> </li> <li class="alt"> <span></span><span class="comment">/* falls through */</span><span> </span> </li> <li><span>statements; </span></li> <li class="alt"> <span></span><span class="keyword">case</span><span> DEF: </span> </li> <li><span> statements; </span></li> <li class="alt"> <span> </span><span class="keyword">break</span><span>; </span> </li> <li> <span></span><span class="keyword">case</span><span> XYZ: </span> </li> <li class="alt"><span>statements; </span></li> <li> <span></span><span class="keyword">break</span><span>; </span> </li> <li class="alt"> <span></span><span class="keyword">default</span><span>: </span> </li> <li><span>statements; </span></li> <li class="alt"> <span></span><span class="keyword">break</span><span>; </span> </li> <li><span>} </span></li> </ol>
每当一个case顺着往下执行时(因为没有break语句),通常应在break语句的位置添加注释。上面的示例代码中就包含注释/* falls through */。
9 try...catch语句
一个try-catch语句应该具有如下格式:
以下为引用的内容:
<ol class="dp-c"> <li class="alt"><span><span>try { </span></span></li> <li><span>statements; </span></li> <li class="alt"><span>} catch (ExceptionClass e) { </span></li> <li><span>statements; </span></li> <li class="alt"><span>} </span></li> </ol>
一个try-catch语句后面也可能跟着一个finally语句,不论try代码块是否顺利执行完,它都会被执行。
以下为引用的内容:
<ol class="dp-c"> <li class="alt"><span><span>try { </span></span></li> <li><span>statements; </span></li> <li class="alt"><span>} catch (ExceptionClass e) { </span></li> <li><span>statements; </span></li> <li class="alt"><span>} finally { </span></li> <li><span>statements; </span></li> <li class="alt"><span>} </span></li> </ol>
希望通过本文的介绍,能够给你带来帮助。