Php 简明教程
PHP – Decision Making
计算机程序默认按照输入-处理-输出路径顺序执行。这种顺序流可以借助所有计算机编程语言(包括 PHP)提供的决策控制语句进行更改。
A computer program by default follows a simple input-process-output path sequentially. This sequential flow can be altered with the decision control statements offered by all the computer programming languages including PHP.
Decision Making in a Computer Program
决策是预测程序执行期间出现的条件并根据条件采取指定操作。
Decision-making is the anticipation of conditions occurring during the execution of a program and specified actions taken according to the conditions.
你可以在代码中使用条件语句来做出决策。实现条件逻辑的能力是编程语言基本要求之一。
You can use conditional statements in your code to make your decisions. The ability to implement conditional logic is one of the fundamental requirements of a programming language.
A Typical Decision Making Structure
以下是大多数编程语言中常见的典型决策结构的一般形式 −
Following is the general form of a typical decision making structure found in most of the programming languages −
Decision Making Statements in PHP
PHP 支持以下三个决策表述:
PHP supports the following three decision making statements −
-
if…else statement − Use this statement if you want to execute a set of code when a condition is true and another if the condition is not true.
-
elseif statement − Use this statement with the if…else statement to execute a set of code if one of the several conditions is true
-
switch statement − If you want to select one of many blocks of code to be executed, use the Switch statement. The switch statement is used to avoid long blocks of if..elseif..else code.
几乎所有编程语言(包括 PHP)都定义了 if-else 语句。这允许有条件地执行代码片段。PHP 中使用 if-else 语句的语法类似于 C 中的语法 −
Almost all the programming languages (including PHP) define the if-else statements. It allows for conditional execution of code fragments. The syntax for using the if-else statement in PHP is similar to that of C −
if (expr)
statement1
else
statement2
此处表达式是布尔表达式,对 true 或 false 进行计算
The expression here is a Boolean expression, evaluating to true or false
-
Any expression involving Boolean operators such as <, >, ⇐, >=, !=, etc. is a Boolean expression.
-
If the expression results in true, the subsequent statement – it may be a simple or a compound statement i.e., a group of statements included in pair of braces – will be executed.
-
If the expression is false, the subsequent statement is ignored, and the program flow continues from next statement onwards.
-
The use of else statement is optional. If the program logic requires another statement or a set of statements to be executed in case the expression (after the if keyword) evaluates to false.
elseif 语句是 if 与 else 的组合。它允许你检查多个条件的 TRUE 值,并在其中一个条件计算结果为 TRUE 时执行一组代码。就像 else 语句一样, elseif 语句是可选的。
The elseif statement is a combination of if and else. It allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE. Just like the else statement, the elseif statement is optional.
switch 语句类似于针对同一表达式的一系列 if 语句。我们将在本教程的后几章详细了解这些语句。
The switch statement is similar to a series of if statements on the same expression. We shall learn about these statements in detail in the subsequent chapters of this tutorial.