PHP之五,循环语句

PHP中,有4种循环语句

while
只要指定的条件成立,则循环执行代码块
do...while
首先执行一次代码块,然后在指定的条件成立时重复这个循环
for
循环执行代码块指定的次数
foreach
根据数组中每个元素来循环代码块


While ,与JAVA的while相同

<?php

$array = array("lm","联盟","bl","部落","xx");
$a = 0;
while($a < 5){
	echo $array[$a]."      ";
	$a++;
}

?>

do...while,与JAVA的do...while相同

<?php

$array = array("lm","联盟","bl","部落","xx");
$a = 0;
do{
	echo $array[$a]."      ";
	$a++;
}
while($a < 5);

?>

for ,我想for循环应该是软件开发中 最常用的循环语句了。PHP 数组的长度需要使用count方法来获得

<?php

$array = array("lm","联盟","bl","部落","xx");
for($i = 0 ; $i<count($array);$i++){
	echo $array[$i];

}

?>

foreach,PHP提供的,方便遍历数组

<?php

$array = array("lm","联盟","bl","部落","xx");

foreach($array as $iii){
	echo $iii."...";

}

?>



你可能感兴趣的:(PHP之五,循环语句)