foreach
foreach循环控制语句主要用于处理数组,是遍历数组的一种简单方法,语法格式为:
froeach(array_expression as $value){
statement;
}
或者
foreach(array_expression as $key=>$value){
statement;
}
参数_arrayexpression是指主要遍历的数组,其中的$value是数组的值,$key是数组的键名称;statement是满足条件时要执行的语句。
<?php
$colors = array("red","green","blue","yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
foreach($colors as $key=>$value){
echo "$key $value <br>"; //输出键与值
}
?>