Home > php教程 > php手册 > PHP基础之介绍PHP的编程语句

PHP基础之介绍PHP的编程语句

WBOY
Release: 2016-06-13 11:14:19
Original
976 people have browsed it

下面介绍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>
Copy after login

2 复合语句

复合语句是包含在大括号中的语句序列,形如"{ 语句 }"。例如下面各段。

  •  被括其中的语句应该较之复合语句缩进一个层次
  •  左大括号"{"应位于复合语句起始行的行尾;右大括号"}"应另起一行并与复合语句首行对齐。
  •  大括号可以被用于所有语句,包括单个语句,只要这些语句是诸如if-else或for控制结构的一部分。这样便于添加语句而无需担心由于忘了加括号而引入bug

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>
Copy after login

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>
Copy after login

注意: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>
Copy after login

注释格式也可以像下面的这种方式写

以下为引用的内容:

<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>
Copy after login

只要可以描述清楚各分支之间的关系,在哪里写注释均可

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>
Copy after login

一个空的for语句(所有工作都在初始化,条件判断,更新子句中完成)应该具有如下格式:

<ol class="dp-c"><li class="alt"><span><span class="keyword">for</span><span> (initialization; condition; update); </span></span></li></ol>
Copy after login

当在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>
Copy after login

一个空的while语句应该具有如下格式:

<ol class="dp-c"><li class="alt"><span><span class="keyword">while</span><span> (condition); </span></span></li></ol>
Copy after login

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>
Copy after login

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>
Copy after login

每当一个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>
Copy after login

一个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>
Copy after login

希望通过本文的介绍,能够给你带来帮助。


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template