PHP program flow control statements _4_1_ jump and termination statement

The following is learning Kong Xiangsheng editor of "PHP Programming Fundamentals tutorial and example" (second edition) notes made.

 

PHP Flow Control There are three types: conditional control structures , loop structures as well as program jump and termination statement .

4.3 Program jumps and termination statement

4.3.1 continue statement

program:

Function: Compute the 3 + 5 + 1 + ... + 99 results.

 1 <?php
 2 $sum = 0;
 3 for($i=1; $i<100; $i++){
 4     if($i%2==0){
 5         continue;   //如果是偶数,就跳出本次循环开始执行下一次循环
 6     }
 7     $sum = $sum + $i;
 8 }
 9 echo $sum;
10 ?>

输出:

1 2500

 

4.3.2 break 语句

程序:

功能:计算1+2+3+...+100的结果。

<?php
$sum = 0;
for($i=1; ;$i++){
    $sum = $sum + $i;
    if($i==100){
        break; 
    }
}
echo $sum;
?>

输出:

1 5050

 

4.3.3 终止PHP程序运行

1.exit 语言结构

程序:

功能:$a变量未定义,则输出字符串信息message,终止PHP代码的执行。

1 <?php
2 @($a) or exit("发生变量未定义错误!");
3 echo "exit 后面的语句将不会运行!";
4 ?>

输出:

发生变量未定义错误!

说明:

1. 使用逻辑或( or )表达式$a or $b, 可以强制只有表达式$a 的结果为FALSE时,表达式$b 才会执行。

2. 当某个表达式运行失败时,该表达式的结果为FALSE。

 

2. die 语言结构

die 可以看作是 exit 的别名。

Guess you like

Origin www.cnblogs.com/xiaoxuStudy/p/11809691.html