Home php教程 php手册 新手入门:学习掌握动态网页PHP的编程语句

新手入门:学习掌握动态网页PHP的编程语句

Jun 21, 2016 am 08:58 AM
else for while

1 简单的语句

每行至多包含一条语句,例如:

$argv++; // 正确的
$argc--; // 正确的
$argv++; $argc--; // 错误的

2 复合语句

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

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

3 返回语句

一个带返回值的return语句不使用小括号"()",除非它们以某种方式使返回值更为显见。例如:

return;
return myDisk.size();
return ($size ? $size : $defaultSize);

4 if与else语句

if-else语句应该具有如下格式:

if (condition){ /* 进行操作的条件 */
  statements;
}
if (condition) {/*进行操作的条件. */
  statements;
} else {/*进行操作的条件*/
  statements;
}
if (condition) {/*进行操作的条件*/
  statements;
} else if (condition) {/*进行操作的条件 */
  statements;
} else{/*进行操作的条件*/
  statements;
}

注意:if语句总是用"{"和"}"括起来,避免使用如下容易引起错误的格式:

if (condition) //避免这种写法,他忽略了“{}”
  statement;

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

if (condition) {
/*进行操作的条件*/
  statements;
} else {
/*进行操作的条件*/
  statements;
}

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

5 for语句

一个for语句应该具有如下格式:

for (initialization; condition; update) {
  statements;
}

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

for (initialization; condition; update);

当在for语句的初始化或更新子句中使用逗号时,避免因使用三个以上变量,而导致复杂度提高。若需要,可以在for循环之前(为初始化子句)或for循环末尾(为更新子句)使用单独的语句。

6 while语句

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

while (condition) {
  statements;
}

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

while (condition);

7 do...while语句

一个do-while语句应该具有如下格式:

do {
  statements;
} while (condition);

8 switch语句

一个switch语句应该具有如下格式:

switch (condition) {
  case ABC:
  /* falls through */
    statements;
  case DEF:
   statements;
   break;
  case XYZ:
    statements;
    break;
  default:
    statements;
    break;
}

每当一个case顺着往下执行时(因为没有break语句),通常应在break语句的位置添加注释。上面的示例代码中就包含注释/* falls through */。

9 try...catch语句

一个try-catch语句应该具有如下格式:

try {
  statements;
} catch (ExceptionClass e) {
  statements;
}

一个try-catch语句后面也可能跟着一个finally语句,不论try代码块是否顺利执行完,它都会被执行。

try {
  statements;
} catch (ExceptionClass e) {
  statements;
} finally {
  statements;
}



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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

17 ways to solve the kernel_security_check_failure blue screen 17 ways to solve the kernel_security_check_failure blue screen Feb 12, 2024 pm 08:51 PM

17 ways to solve the kernel_security_check_failure blue screen

In C language, what is the difference between while(1) and while(0)? In C language, what is the difference between while(1) and while(0)? Aug 31, 2023 am 10:45 AM

In C language, what is the difference between while(1) and while(0)?

How to uninstall Skype for Business on Win10? How to completely uninstall Skype on your computer How to uninstall Skype for Business on Win10? How to completely uninstall Skype on your computer Feb 13, 2024 pm 12:30 PM

How to uninstall Skype for Business on Win10? How to completely uninstall Skype on your computer

How to use for to find the factorial of n in JavaScript How to use for to find the factorial of n in JavaScript Dec 08, 2021 pm 06:04 PM

How to use for to find the factorial of n in JavaScript

Usage of while Usage of while Sep 25, 2023 am 09:47 AM

Usage of while

What is the difference between foreach and for loop What is the difference between foreach and for loop Jan 05, 2023 pm 04:26 PM

What is the difference between foreach and for loop

Is while a keyword in go language? Is while a keyword in go language? Jun 04, 2021 pm 05:01 PM

Is while a keyword in go language?

What are the common flow control structures in Python? What are the common flow control structures in Python? Jan 20, 2024 am 08:17 AM

What are the common flow control structures in Python?

See all articles