php基础语法——循环语句

八、循环语句

1、while 循环

while (条件为真) {
  要执行的代码;
}

2、do...while 循环

do {
  要执行的代码;
} while (条件为真);

3、for 循环

for (init counter; test counter; increment counter) {
  code to be executed;
}
<?php 
for ($x=0; $x<=10; $x++) {
  echo "数字是:$x <br>";
} 
?>

4、foreach 循环

foreach ($array as $value) {
  code to be executed;
}
<?php 
$colors = array("red","green","blue","yellow"); 

foreach ($colors as $value) {
  echo "$value <br>";
}
?>

猜你喜欢

转载自blog.csdn.net/qq_42133100/article/details/97395475