php初级讲义7-流程控制

流程控制的概念

通过流程控制可以实现抽象的逻辑需求,可以有条件地控制代码的执行,可以自由地对代码进行循环执行来实现数据的读取和处理。

条件控制结构

条件控制结构主要包括ifswitch两种。

$x = 5;
$y = 3;

if($x > $y)
    echo '$x is bigger than $y'; // $x is bigger than $y

echo '
'; if ($x > $y) echo '$x is bigger than $y'; // $x is bigger than $y echo '
'; if($x > $y){ echo '$x is bigger than $y'; // $x is bigger than $y } echo '
'; if ($x > $y) { echo '$x is bigger than $y'; // $x is bigger than $y } echo '
'; if ($x > $y): echo '$x is bigger than $y'; // $x is bigger than $y endif; echo '
'; if ($x > $y) { echo '$y is smaller than $x'; // $y is smaller than $x } echo '
'; if ($x > $y) { echo '$x is bigger than $y'; // $x is bigger than $y echo '
'; echo '$y is smaller than $x'; // $x is smaller than $y } $y = 6; if ($x > $y) echo '$x is bigger than $y'; // 没有输出 echo '
'; echo '$y is smaller than $x'; // $y is smaller than $x echo '
'; if ($x < $y): echo '$x is smaller than $y'; // $x is smaller than $y echo '
'; echo '$y is bigger than $x'; // $y is bigger than $x endif; echo '
'; $z = 7; if ($x < $y) if ($y < $z) echo '$z is the biggest'; // $z is the biggest echo '
'; if ($x < $y) { if ($y < $z) { echo '$z is the biggest'; // $z is the biggest } } echo '
';
  • if条件控制结构使得代码可以根据指定条件有选择地进行执行。
  • if条件中的表达式取得的值为布尔值,程序会根据该值有选择地执行代码。
  • if条件控制结构中如果某个执行段的语句数大于1则需要用{}进行围绕。
  • if条件控制结构可以进行嵌套。
$x = 5;
$y = 7;
if ($x > $y) {
    echo '$x is bigger than $y';
} else {
    echo '$x is not bigger than $y';
}
// $x is not bigger than $y
echo '
'; if ($x > $y) echo '$x is bigger than $y'; else echo '$x is not bigger than $y'; // $x is not bigger than $y echo '
'; if ($x > $y): echo '$x is bigger than $y'; else: echo '$x is not bigger than $y'; endif; // $x is not bigger than $y echo '
'; if ($x < $y) { echo '$x is smaller than $y'; } else { echo '$x is not smaller than $y'; echo '$x may be equal to $y'; } // $x is smaller than $y echo '
'; if ($x < $y) echo '$x is smaller than $y'; else echo '$x is not smaller than $y'; echo '
'; echo '$x may be equal to $y'; // $x is smaller than $y // $x may be equal to $y echo '
'; if ($x < $y): echo '$x is smaller than $y'; else: echo '$x is not smaller than $y'; echo '
'; echo '$x may be equal to $y'; endif; // $x is smaller than $y echo '
';
  • if...else...中的else结构定义了条件不成立时所要执行的代码片段。
$x = 5;
$y = 7;
if ($x > $y) {
    echo '$x is bigger than $y';
} else if ($x == $y) {
    echo '$x is equal to $y';
} else {
    echo '$x is smaller than $y';
}
// $x is smaller than $y
echo '
'; if ($x > $y) echo '$x is bigger than $y'; else if ($x == $y) echo '$x is equal to $y'; else echo '$x is smaller than $y'; // $x is smaller than $y echo '
'; /*if ($x > $y): echo '$x is bigger than $y'; else if ($x == $y): // 网页无法正常运作 echo '$x is equal to $y'; else: echo '$x is smaller than $y'; echo '
';*/ if ($x > $y): echo '$x is bigger than $y'; elseif ($x == $y): echo '$x is equal to $y'; else: echo '$x is smaller than $y'; endif; // $x is smaller than $y echo '
'; if ($x < $y) { echo '$x is smaller than $y'; } else if ($x == $y) { echo '$x is equal to $y'; echo '
'; echo '$x = $y'; } else { echo '$x is bigger than $y'; echo '
'; echo '$x = $y'; } // $x is smaller than $y echo '
'; if ($x < $y) echo '$x is smaller than $y'; else if ($x == $y) echo '$x is equal to $y'; // echo '$x = $y'; // 网页无法正常运作 else echo '$x is bigger than $y'; echo '
'; echo '$x = $y'; // $x is smaller than $y // $x = $y echo '
'; if ($x < $y): echo '$x is smaller than $y'; elseif ($x == $y): echo '$x is equal to $y'; // echo '$x = $y'; else: echo '$x is bigger than $y'; echo '
'; echo '$x = $y'; endif; // $x is smaller than $y echo '
'; if (1 == $x) { echo '$x is equal to 1'; echo '
'; } else if (2 == $x) { echo '$x is equal to 2'; echo '
'; } else if (3 == $x) { echo '$x is equal to 3'; echo '
'; } else if (4 == $x) { echo '$x is equal to 3'; echo '
'; } else if (5 == $x) { echo '$x is equal to 5'; echo '
'; } else { echo '$x is bigger than 5'; echo '
'; } // $x is equal to 5 if (1 == $x): echo '$x is equal to 1'; echo '
'; elseif (2 == $x): echo '$x is equal to 2'; echo '
'; elseif (3 == $x): echo '$x is equal to 3'; echo '
'; elseif (4 == $x): echo '$x is equal to 3'; echo '
'; elseif (5 == $x): echo '$x is equal to 5'; echo '
'; else: echo '$x is bigger than 5'; echo '
'; endif; // $x is equal to 5
  • else ifelseif可以用于多重条件控制,当该结构前面的ifelse ifelseif)都为假且当前的结构中的表达式为真时则执行这一控制结构中的代码。
  • else ifelseif的功能在用花括号包围结构代码时没有区别的,但当冒号定义的添加中只能用elseif,否则会产生解析错误。
$x = 5;
switch($x){
    case 0:
        echo '$x is equal to 0';
        break;
    case 1:
        echo '$x is equal to 1';
        break;
    case 2:
        echo '$x is equal to 2';
        break;
    case 3:
        echo '$x is equal to 3';
        break;
    case 4:
        echo '$x is equal to 4';
        break;
    case 5:
        echo '$x is equal to 5';
        break;
}
// $x is equal to 5
echo '
'; $x++; switch($x){ case 0: echo '$x is equal to 0'; break; case 1: echo '$x is equal to 1'; break; case 2: echo '$x is equal to 2'; break; case 3: echo '$x is equal to 3'; break; case 4: echo '$x is equal to 4'; break; case 5: echo '$x is equal to 5'; break; default: echo '$x is bigger than 5'; break; } // $x is bigger than 5 echo '
'; switch ($x) { case 0: echo '$x is equal to 0'; break; case 1: echo '$x is equal to 1'; break; case 2: echo '$x is equal to 2'; break; case 3: echo '$x is equal to 3'; break; case 4: echo '$x is equal to 4'; break; case 5: echo '$x is equal to 5'; break; default: echo '$x is bigger than 5'; break; } // $x is bigger than 5 echo '
'; switch ($x) { case 0: echo '$x is equal to 0'; echo '
'; break; case 1: echo '$x is equal to 1'; echo '
'; break; case 2: echo '$x is equal to 2'; echo '
'; break; case 3: echo '$x is equal to 3'; echo '
'; break; case 4: echo '$x is equal to 4'; echo '
'; break; case 5: echo '$x is equal to 5'; echo '
'; break; default: echo '$x is bigger than 5'; echo '
'; break; } // $x is bigger than 5 switch($x): case 0: echo '$x is equal to 0'; echo '
'; break; case 1: echo '$x is equal to 1'; echo '
'; break; case 2: echo '$x is equal to 2'; echo '
'; break; case 3: echo '$x is equal to 3'; echo '
'; break; case 4: echo '$x is equal to 4'; echo '
'; break; case 5: echo '$x is equal to 5'; echo '
'; break; default: echo '$x is bigger than 5'; echo '
'; break; endswitch; // $x is bigger than 5 switch ($x): case 0: echo '$x is equal to 0'; echo '
'; break; case 1: echo '$x is equal to 1'; echo '
'; break; case 2: echo '$x is equal to 2'; echo '
'; break; case 3: echo '$x is equal to 3'; echo '
'; break; case 4: echo '$x is equal to 4'; echo '
'; break; case 5: echo '$x is equal to 5'; echo '
'; break; default: echo '$x is bigger than 5'; echo '
'; break; endswitch; // $x is bigger than 5 switch ($x) { case 0; echo '$x is equal to 0'; echo '
'; break; case 1; echo '$x is equal to 1'; echo '
'; break; case 2: echo '$x is equal to 2'; echo '
'; break; case 3; echo '$x is equal to 3'; echo '
'; break; case 4; echo '$x is equal to 4'; echo '
'; break; case 5; echo '$x is equal to 5'; echo '
'; break; default; echo '$x is bigger than 5'; echo '
'; break; } // $x is bigger than 5 $x = 1; switch ($x) { case 0; echo '$x is equal to 0'; echo '
'; case 1; echo '$x is equal to 1'; echo '
'; case 2: echo '$x is equal to 2'; echo '
'; case 3; echo '$x is equal to 3'; echo '
'; break; case 4; echo '$x is equal to 4'; echo '
'; break; case 5; echo '$x is equal to 5'; echo '
'; break; default; echo '$x is bigger than 5'; echo '
'; break; } // $x is equal to 1 // $x is equal to 2 // $x is equal to 3 switch ($x) { default; echo '$x is bigger than 5'; echo '
'; break; case 0; echo '$x is equal to 0'; echo '
'; case 1; echo '$x is equal to 1'; echo '
'; case 2: echo '$x is equal to 2'; echo '
'; case 3; echo '$x is equal to 3'; echo '
'; break; case 4; echo '$x is equal to 4'; echo '
'; break; case 5; echo '$x is equal to 5'; echo '
'; break; } /* $x is equal to 1 $x is equal to 2 $x is equal to 3 */ $x = 8; switch ($x) { default; echo '$x is bigger than 5'; echo '
'; break; case 0; echo '$x is equal to 0'; echo '
'; case 1; echo '$x is equal to 1'; echo '
'; case 2: echo '$x is equal to 2'; echo '
'; case 3; echo '$x is equal to 3'; echo '
'; break; case 4; echo '$x is equal to 4'; echo '
'; break; case 5; echo '$x is equal to 5'; echo '
'; break; } // $x is bigger than 5 $x = 3; switch ($x) { default; echo '$x is bigger than 5'; echo '
'; break; case 0; echo '$x is equal to 0'; echo '
'; case 1; echo '$x is equal to 1'; echo '
'; case 2: echo '$x is equal to 2'; echo '
'; case 3; echo '$x is equal to 3'; echo '
'; break; case 4; echo '$x is equal to 4'; echo '
'; break; case 5; echo '$x is equal to 5'; echo '
'; break; } // $x is equal to 3 switch ($x) { default; echo '$x is bigger than 5'; echo '
'; break; case 5; echo '$x is equal to 5'; echo '
'; break; case 0; echo '$x is equal to 0'; echo '
'; case 1; echo '$x is equal to 1'; echo '
'; case 4; echo '$x is equal to 4'; echo '
'; break; case 2: echo '$x is equal to 2'; echo '
'; case 3; echo '$x is equal to 3'; echo '
'; break; } // $x is equal to 3 $name = 'stone'; switch($name){ case 'andy': echo 'my name is andy'; break; case 'jack': echo 'my name is jack'; break; case 'stone': echo 'my name is stone'; break; case 'lucy': echo 'my name is lucy'; break; default: echo 'I have no name'; break; } // my name is stone echo '
'; $name = 'Stone'; switch($name){ case 'andy': echo 'my name is andy'; break; case 'jack': echo 'my name is jack'; break; case 'STONE': case 'Stone': case 'stone': echo 'my name is stone'; break; case 'lucy': echo 'my name is lucy'; break; default: echo 'I have no name'; break; } // my name is stone echo '
';

循环控制结构

php中,常用的循环控制结构包括while, do-while, for以及foreach

$i = 0;
while ($i <= 9) {
    echo $i;
    $i++;
}
// 0123456789
echo '
'; // 当while表达式的值为TRUE时执行循环体中的内容,while表达式的值在开始循环时检查,所以while循环有可能一次都不执行 $i = 0; while($i <= 9){ echo $i; $i++; } // 0123456789 echo '
'; $i = 0; while ($i <= 9): echo $i; $i++; endwhile; // 0123456789 echo '
'; $i = 0; while($i <= 9): echo $i; $i++; endwhile; // 0123456789 echo '
'; $i = 0; while ($i <= 9) $i++; echo $i; // 10 echo '
'; $i = 0; do { echo $i; $i++; } while ($i < 10); // 0123456789 echo '
'; // do-while循环每次在结束时检查循环表达式,所以此种循环至少会执行一次 $i = 0; do $i++; while ($i < 10); echo $i; // 10 echo '
'; $i = 0; do{ echo $i; $i++; }while($i < 10); // 0123456789 echo '
'; $i = 0; do { if ($i > 9) { break; } echo $i++; } while (TRUE); // 0123456789 // 可以不控制循环表达式,在循环体内通过break跳出循环 echo '
'; for ($i = 0;$i < 10;$i++) { echo $i; } // 0123456789 // for循环的结构为:for (expr1; expr2; expr3) statement, 其中每个表达式都可以为空或是用逗号分隔的多个表达式。expr1和expr2在循环开始前一定会执行一次。expr2如果为真则执行循环体,否则终止循环,当有多个表达式用逗号分隔时以最后一个表达的值为准。expr3在每次循环后执行。 echo '
'; for ($i = 0;$i < 10;$i++) echo $i; // 0123456789 // 当循环体只有一条语句时可以省略花括号,但是这个做是不推荐的,始终带有花括号的循环结构看起来更清晰 echo '
'; for ($i = 0;;$i++) { if ($i > 9) { break; } echo $i; } // 0123456789 echo '
'; $i = 0; for (;;) { if ($i > 9) { break; } echo $i++; } // 0123456789 echo '
'; // for ($i = 0, $j = 0;$j = $i, $i < 10;$j++, echo $j, echo '
', echo $i, $i++); // Parse error: syntax error, unexpected 'echo' (T_ECHO) for ($i = 0, $j = 0;$j = $i, $i < 10;$j++, print $j, print '
', print $i, $i++); /* 1 02 13 24 35 46 57 68 79 810 9 */ echo '
'; for ($i = 0;$i < 10;$i++): echo $i; endfor; // 0123456789 echo '
'; // foreach提供一种用于遍历数据或对象的方式 $four_kings = ['刘德华', '张学友', '黎明', '郭富城']; foreach ($four_kings as $value) { echo $value; echo '
'; } /* 刘德华 张学友 黎明 郭富城 */ foreach ($four_kings as $value) echo $value; echo '
'; // 刘德华张学友黎明郭富城 foreach ($four_kings as $key => $value) { echo 'key:'.$key.' => value:'.$value; echo '
'; } /* key:0 => value:刘德华 key:1 => value:张学友 key:2 => value:黎明 key:3 => value:郭富城 */ // foreach开始执行时,数组内部的指针会自动指向第一个单元,不需要像其它循环遍历那样在循环前调用reset() // reset()是一个函数,用于将数组内部指针指向第一个单元 while (list($key, $value) = each($four_kings)) { echo 'key:'.$key.' => value:'.$value; echo '
'; if ($key > 1) { break; } } /* key:0 => value:刘德华 key:1 => value:张学友 key:2 => value:黎明 */ while (list($key, $value) = each($four_kings)) { echo 'key:'.$key.' => value:'.$value; echo '
'; } // key:3 => value:郭富城 // each()函数用于返回数组中当前的键值对并将数组指针向前移一步 // list()函数用于把数组中的值赋值给一些变量 foreach ($four_kings as $key => $value) { echo 'key:'.$key.' => value:'.$value; echo '
'; if ($key > 1) { break; } } /* key:0 => value:刘德华 key:1 => value:张学友 key:2 => value:黎明 */ echo '
'; foreach ($four_kings as $key => $value) { echo 'key:'.$key.' => value:'.$value; echo '
'; } /* key:0 => value:刘德华 key:1 => value:张学友 key:2 => value:黎明 key:3 => value:郭富城 */ while (list($key, $value) = each($four_kings)) { echo 'key:'.$key.' => value:'.$value; echo '
'; if ($key > 1) { break; } } // 此处输出为空,如果不在意$key则while中的表达式可以写成list(, $value) = each($four_kings) reset($four_kings); while (list($key, $value) = each($four_kings)) { echo 'key:'.$key.' => value:'.$value; echo '
'; if ($key > 1) { break; } } /* key:0 => value:刘德华 key:1 => value:张学友 key:2 => value:黎明 */ reset($four_kings); while (list($key, $value) = each($four_kings)) { echo 'key:'.$key.' => value:'.$value; echo '
'; } /* key:0 => value:刘德华 key:1 => value:张学友 key:2 => value:黎明 key:3 => value:郭富城 */ echo '
'; $arr = [ 'zero' => [0, 1, 2 ,3], 'two' => [4, 5, 6 ,7], 'three' => [8, 9], ]; foreach ($arr as $key => $value) { echo 'key:'.$key.'
'; foreach ($value as $k => $v) { echo 'k='.$k.' => v='.$v; echo '
'; } } /* key:zero k=0 => v=0 k=1 => v=1 k=2 => v=2 k=3 => v=3 key:two k=0 => v=4 k=1 => v=5 k=2 => v=6 k=3 => v=7 key:three k=0 => v=8 k=1 => v=9 */ $arr = [ 'zero' => [0, 1, 2], 'two' => [3, 4, 5], 'three' => [6, 7, 8], ]; foreach ($arr as list($i, $j, $k)) { echo '$i:'.$i.', $j:'.$j.', $k:'.$k; echo '
'; } /* $i:0, $j:1, $k:2 $i:3, $j:4, $k:5 $i:6, $j:7, $k:8 */ foreach ($arr as list(, $j, $k, $m)) { echo '$j:'.$j.', $k:'.$k.', $m:'.$m; echo '
'; } /* Notice: Undefined offset: 3 $j:1, $k:2, $m: Notice: Undefined offset: 3 $j:4, $k:5, $m: Notice: Undefined offset: 3 $j:7, $k:8, $m: */

流程跳转结构

流程跳转结构包括break, continue, gotobreak表示跳出循环,continue表示继续循环,goto表示跳转到指定的位置。

$arr = [1, 2, 3, 4];
foreach ($arr as $key => $value) {
    if ($key == 2) {
        break;
    }
    echo $value;
}
// 12
echo '
'; foreach ($arr as $key => $value) { if ($key == 2) { continue; } echo $value; } // 124 echo '
'; $arr = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], ]; foreach ($arr as $key => $value) { foreach ($value as $k => $v) { if ($k == 1) { break; } if ($key == 2) { break 2; } echo $v; } echo '
'; } /* 0 3 */ echo '
'; foreach ($arr as $key => $value) { foreach ($value as $k => $v) { if ($k == 1) { continue; } if ($key == 1) { continue 2; } echo $v; } echo '
'; } /* 02 68 */ echo '
'; goto another; echo 'this is a echo'; echo '
'; another: echo 'this is another echo'; // this is another echo echo '
'; foreach ($arr as $key => $value) { foreach ($value as $k => $v) { if ($k == 1) { continue; } if ($key == 1) { goto after_arr; } echo $v; } echo '
'; } after_arr: echo 'this is echo after arr'; /* 02 this is echo after arr */ goto loop; // Fatal error: 'goto' into loop or switch statement is disallowed echo 'there is a loop below'; foreach ($arr as $key => $value) { foreach ($value as $k => $v) { echo $v; } loop: echo '
'; } echo 'there is a loop uppon';

文件包含结构

常用的文件包含结构包括include, require, include_once, require_once

// test.php
$x = 5;
var_dump($x); // NULL
echo '
'; include 'test.php'; echo $x; // 5 echo '
'; ini_set('display_errors', 'on'); error_reporting(E_ALL); include 'stone.php'; // Warning: include(stone.php): failed to open stream: No such file or directory // Warning: include(): Failed opening 'stone.php' for inclusion (include_path='.:')
// test.php
ini_set('display_errors', 'on');
$x = 5;
echo $y;
echo '
'; echo $z;
$y = 6;
include 'test.php';
echo $x;
$z = 7;
/*
6

Notice: Undefined variable: z 
5
*/
// test.php
$x = 5;
$y = include 'test.php';
echo $x; // 5
echo '
'; var_dump($y); echo '
'; ini_set('display_errors', 'on'); $z = include 'stone.php'; var_dump($z); echo '
'; /* 5 int(1) Warning: include(stone.php): failed to open stream: No such file or directory Warning: include(): Failed opening 'stone.php' for inclusion (include_path='.:') bool(false) */
$y = require 'test.php';
echo $x; // 5
echo '
'; var_dump($y); echo '
'; ini_set('display_errors', 'on'); $z = require 'stone.php'; var_dump($z); echo '
'; /* 5 int(1) Warning: require(stone.php): failed to open stream: No such file or directory Fatal error: require(): Failed opening required 'stone.php' (include_path='.:') */ // require除了在错误处理上不同之外,和include是一样的。当被包含的文件不存在时,include会报一个警告错误,脚本会继续运行,而require会报致命错误(有的版本可能是编译错误)同时终止脚本的运行。

include_onceinclude的使用是一致的,唯一的区别是include_once会判断文件是否已经被包含,如果已经被包含,则不会再次包含。require_oncerequire的使用是一致的,唯一的区别是require_once会判断文件是否已经被包含,如果已经被包含,则不会再次包含。

本文首发于公众号:programmer_cc,转载请注明出处。


php初级讲义7-流程控制_第1张图片
微信公众号.jpg

你可能感兴趣的:(php初级讲义7-流程控制)