Php 简明教程
PHP - If…Else Statement
实现条件逻辑的能力是任何编程语言(包括 PHP)的基本要求。PHP 有三个关键字(也称为 language constructs) – if, elseif 和 else – 根据不同的条件做出决策。
The ability to implement conditional logic is the fundamental requirement of any programming language (PHP included). PHP has three keywords (also called as language constructs) – if, elseif and else – are used to take decision based on the different conditions.
if 关键字是代码片段条件执行的基本结构。 if 关键字通常与 else 关键字结合使用,尽管它并不总是必需的。
The if keyword is the basic construct for the conditional execution of code fragments. More often than not, the if keyword is used in conjunction with else keyword, although it is not always mandatory.
如果你想在条件为 true 时执行一些代码,在相同条件为 false 时执行其他代码,则使用 “if….else” 语句。
If you want to execute some code if a condition is true and another code if the sme condition is false, then use the "if….else" statement.
Syntax
%{s8} 语句在 PHP 中的使用和语法类似于 C 语言。以下是 PHP 中 if 语句的语法 −
The usage and syntax of the if statement in PHP is similar to that of the C language. Here is the syntax of if statement in PHP −
if (expression)
code to be executed if expression is true;
else
code to be executed if expression is false;
if 语句总后跟一个布尔表达式。
The if statement is always followed by a Boolean expression.
-
PHP will execute the statement following the Boolean expression if it evaluates to true.
-
If the Boolean expression evaluates to false, the statement is ignored.
-
If the algorithm needs to execute another statement when the expression is false, it is written after the else keyword.
Example
以下是一个演示 if else 语句用法的简单 PHP 代码。有两个变量 $a 和 $b。该代码识别出它们中的哪一个更大。
Here is a simple PHP code that demonstrates the usage of if else statements. There are two variables $a and $b. The code identifies which one of them is bigger.
<?php
$a=10;
$b=20;
if ($a > $b)
echo "a is bigger than b";
else
echo "a is not bigger than b";
?>
运行上述代码时,它会显示以下 output −
When the above code is run, it displays the following output −
a is not bigger than b
交换 “a” 和 “b” 的值,然后重新运行。现在,你将获得以下输出 −
Interchange the values of "a" and "b" and run again. Now, you will get the following output −
a is bigger than b
Example
如果当前是星期五,下列示例将输出“Have a nice weekend!”,否则将输出“Have a nice day!” −
The following example will output "Have a nice weekend!" if the current day is Friday, else it will output "Have a nice day!" −
<?php
$d = date("D");
if ($d == "Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
它将生成以下 output −
It will produce the following output −
Have a nice weekend!
Using endif in PHP
PHP 代码通常与 HTML 脚本混合在一起。我们可以在 if 部分和 PHP 代码中的 else 部分插入 HTML 代码。PHP 为 if 和 else 语句提供了替代语法。将左大括号改为冒号 (:),将右大括号改为 endif; ,这样可以将 HTML 块添加到 if 和 else 部分。
PHP code is usually intermixed with HTML script. We can insert HTML code in the if part as well as the else part in PHP code. PHP offers an alternative syntax for if and else statements. Change the opening brace to a colon (:) and the closing brace to endif; so that a HTML block can be added to the if and else part.
<?php
$d = date("D");
if ($d == "Fri"): ?>
<h2>Have a nice weekend!</h2>
<?php else: ?>
<h2>Have a nice day!</h2>
<?php endif ?>
确保上述脚本位于 PHP 服务器的文档根目录中。访问 URL http://localhost/hello.php 。如果当前不是星期五,浏览器中应显示以下输出 −
Make sure that the above script is in the document root of PHP server. Visit the URL http://localhost/hello.php. Following output should be displayed in the browser, if the current day is not a Friday −
Have a nice day!
Using elseif in PHP
如果某几个条件中有一个条件是真的,而您希望执行一些代码,则使用 elseif 语句。PHP 中的 elseif 语言结构是 if 和 else 的组合。
If you want to execute some code if one of the several conditions are true, then use the elseif statement. The elseif language construct in PHP is a combination of if and else.
-
Similar to else, it specifies an alternative statement to be executed in case the original if expression evaluates to false.
-
However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true.
if (expr1)
code to be executed if expr1 is true;
elseif (expr2)
code to be executed if expr2 is true;
else
code to be executed if expr2 is false;
Example
让我们修改上述代码以在星期日、星期五和其他日子显示不同的消息。
Let us modify the above code to display a different message on Sunday, Friday and other days.
<?php
$d = date("D");
if ($d == "Fri")
echo "<h3>Have a nice weekend!</h3>";
elseif ($d == "Sun")
echo "<h3>Have a nice Sunday!</h3>";
else
echo "<h3>Have a nice day!</h3>";
?>
在星期日,浏览器应显示以下 output −
On a Sunday, the browser shall display the following output −
Have a nice Sunday!
Example
这里有另一个示例,说明 if–elselif–else 语句的用法 −
Here is another example to show the use of if–elselif–else statements −
<?php
$x=13;
if ($x%2==0) {
if ($x%3==0)
echo "<h3>$x is divisible by 2 and 3</h3>";
else
echo "<h3>$x is divisible by 2 but not divisible by 3</h3>";
}
elseif ($x%3==0)
echo "<h3>$x is divisible by 3 but not divisible by 2</h3>";
else
echo "<h3>$x is not divisible by 3 and not divisible by 2</h3>";
?>
上述代码还使用 nestedif 语句。
The above code also uses nestedif statements.
当 x 的值为 13、12 和 10 时, output 如下 −
For the values of x as 13, 12 and 10, the output will be as follows −
13 is not divisible by 3 and not divisible by 2
12 is divisible by 2 and 3
10 is divisible by 2 but not divisible by 3