Correcting teacher:灭绝师太
Correction status:qualified
Teacher's comments:
1.for
2.forEach
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>for forEach遍历数组</title>
</head>
<body>
<script>
let arr=[1,2,3,4];
//for遍历数组
for(let i=0;i<arr.length;i++){
arr[i];
}
//forEach遍历数组
arr.forEach(function(item){
alert(item);
})
</script>
</body>
</html>
<!-- php数组的遍历 -->
$cars=['奥迪','宝马','奔驰'];
<!-- php for遍历数组 -->
for($i=0;$i<count($cars);$i++){
echo $cars[$i].'<br/>';
}
<!-- php forEach遍历 -->
forEach($cars as $car){
echo $car.'<br/>';
}
<!-- 带有key值的 php forEach遍历 -->
forEach($cars as $k=> $car){
echo $k.'=>'.$car.'<br/>';
}
<!-- 放在li标签中 -->
<?php forEach($cars as $k=>$car){
echo '<li><a href="">'.$car.'</a></li>';
}
>
1.js在遍历数组时候要声明变量而php可以用$直接使用。
2.js用length属性获取数组的长度,而php用count()函数获取。