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;
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor