nblogs-markdown">
title: PHP循环语句
date: 2018-5-18
categories:
PHP学习笔记
tags:php
foeach语句
<?php header("Content-type:text/html;charset=utf-8"); $stu = array('中文昵称' => '沫沫', '英文昵称' => 'Mo', '外号' => '小沫猪'); foreach ($stu as $key => $value) { echo $key.'----'.$value.'<br>'; } $swt = array('今天', '天气', '真热!'); foreach ($swt as $value) { echo $value; } ?>
嵌套循环
<?php header("Content-type:text/html;charset=utf-8"); echo "<br>--------嵌套的while循环--------<br>"; $num = 1; while ($num <= 10) { $i = 1; while ($i <= $num) { echo $i.' '; ++$i; } echo '<br>'; ++$num; } echo "<br>--------while嵌入for循环--------<br>"; $num = 1; while ($num <= 10) { for($i = 1; $i <= $num; ++$i){ echo $i.' '; } echo '<br>'; ++$num; } echo "<br>--------for嵌入while循环--------<br>"; for($num = 1; $num <= 10; ++$num){ $i = 1; while ($i <= $num) { echo $i.' '; ++$i; } echo '<br>'; } echo "<br>--------for嵌入for循环--------<br>"; for($num = 1; $num <= 10; ++$num){ for($i = 1; $i <= $num; ++$i){ echo $i.' '; } echo '<br>'; } echo "<br>--------嵌套的foreach循环--------<br>"; $students = array( array('name' => '张三', 'age' => 20, 'sex' => '男') ); array('name' => '李四', 'age' => 23, 'sex' => '男') ); foreach ($students as $stu) { echo '<p>'; foreach ($stu as $key => $value) { echo $key.'---'.$value.'<br>'; } } ?>
循环遍历
<?php header("Content-type:text/html;charset=utf-8"); $id_list = [1, 5, 4, 6]; $name_list = ["jack", "tom", "张三", "李四"]; $age_list = [12, 11, 22, 24]; $address_list = ["江西赣州", "江西九江", "江西南昌", "江西吉安"]; $classNO_list = ["B1631", "B1632", "A1666", "C1444"]; echo "-----for循环遍历------"; echo '</br>'; $len = count($id_list); for($i=0; $i<$len; $i++){ echo $id_list[$i]; } echo '</br>'; echo '</br>'; echo "-----while循环遍历------"; echo '</br>'; $len = count($id_list); $i = 0; //这里i必须从0开始,并且while里面必须是++i;否则第一个元素不能被遍历到 while ($i < $len) { echo $id_list[$i]; ++$i; } echo '</br>'; echo '</br>'; echo "-----list()和each()循环遍历------"; echo '</br>'; while (list($key, $value) = each($address_list)) { echo $key.'--->'.$value; echo '</br>'; } echo '</br>'; echo '</br>'; echo "-----var_dump()函数------"; echo '</br>'; var_dump($name_list); ?>