我对for/while/foreach()遍历的不同之处

Original 2019-05-15 21:39:16 173
abstract:<?php$min=1;$max=20; $date=range($min, $max); $con=count($date);/*for($i=0;$i<$con;$i++){$res =$date[$i];echo $res.'<hr>';}*//*$n=0;while ($n <$con) {$res =$date[$n];e

<?php

$min=1;

$max=20;

 $date=range($min, $max);

 $con=count($date);

/*for($i=0;$i<$con;$i++){

$res =$date[$i];

echo $res.'<hr>';

}*/

/*$n=0;

while ($n <$con) {

$res =$date[$n];

echo $res.'<hr>';

$n++;

}*/

foreach ($date as  $value) {

echo $value.'<hr>';

}

?>

  1. for,while:for,while循环要对数组进行遍历,首先要知道定义变量来获取数组的长度,以用于判断for循环循环几次,获取第几次循环的值。不同之处在于自动+1,for循环自动+1的变量,在于小括号的条件里面;while循环自动+1的变量,在于大括号的条件里面;没有该条件,for会报错,while会陷入死循环

  2. foreach 不用知道数组的长度,直接定义变量 $value,就可以对数组进行遍历,会有一个关键字as

Correcting teacher:查无此人Correction time:2019-05-16 09:11:31
Teacher's summary:完成的不错。for和while是计算循环,foreach是数组循环,继续加油

Release Notes

Popular Entries