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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

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

Kernelsecuritycheckfailure (kernel check failure) is a relatively common type of stop code. However, no matter what the reason is, the blue screen error causes many users to be very distressed. Let this site carefully introduce 17 types to users. Solution. 17 solutions to kernel_security_check_failure blue screen Method 1: Remove all external devices When any external device you are using is incompatible with your version of Windows, the Kernelsecuritycheckfailure blue screen error may occur. To do this, you need to unplug all external devices before trying to restart your computer.

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

We know that in C language, 'while' keyword is used to define a loop that works based on the condition passed to the loop. Now, since the condition can have two values, true or false, the code inside the while block will be executed repeatedly if the condition is true and will not be executed if the condition is false. Now, by passing parameters to the while loop, we can differentiate between while(1) and while(0) because while(1) is a loop where the condition is always considered true and hence the code inside the block will start executing repeatedly. Furthermore, we can state that it is not 1 that is passed to the loop that makes the condition true, but if any non-zero integer is passed to the while loop, then it will be considered as the true condition, so

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

Can Win10 skype be uninstalled? This is a question that many users want to know, because many users find that this application is included in the default program on their computers, and they are worried that deleting it will affect the operation of the system. Let this website help users Let’s take a closer look at how to uninstall Skype for Business in Win10. How to uninstall Skype for Business in Win10 1. Click the Windows icon on the computer desktop, and then click the settings icon to enter. 2. Click "Apply". 3. Enter "Skype" in the search box and click to select the found result. 4. Click "Uninstall". 5

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 n factorial: 1. Use the "for (var i=1;i<=n;i++){}" statement to control the loop traversal range to "1~n"; 2. In the loop body, use "cj *=i" Multiply the numbers from 1 to n, and assign the product to the variable cj; 3. After the loop ends, the value of the variable cj is the factorial of n, and then output it.

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

Difference: 1. for loops through each data element through the index, while forEach loops through the data elements of the array through the JS underlying program; 2. for can terminate the execution of the loop through the break keyword, but forEach cannot; 3. for can control the execution of the loop by controlling the value of the loop variable, but forEach cannot; 4. for can call loop variables outside the loop, but forEach cannot call loop variables outside the loop; 5. The execution efficiency of for is higher than forEach.

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

In the Go language, while is not a keyword. You can use the for statement plus break to achieve the effect of a while loop, such as "for {sum++ if sum>10{break}else{...}}". The go language has 25 keywords such as break, default, func, select, case, defer, go, map, else, goto, for, if, var, etc.

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

The usage of while is "while condition: code block". The condition is an expression. When the condition is true, the code block is executed, and then it is judged again whether the condition is true. If it is true, the code block continues to be executed until the condition is false. . while is a commonly used loop control statement, used to repeatedly execute a block of code when certain conditions are met.

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? In Python, the flow control structure is an important tool used to determine the execution order of the program. They allow us to execute different blocks of code based on different conditions, or to execute a block of code repeatedly. The following will introduce common process control structures in Python and provide corresponding code examples. Conditional statements (if-else): Conditional statements allow us to execute different blocks of code based on different conditions. Its basic syntax is: if condition 1: #when condition

See all articles