php 循环的使用 (php 99乘法表)
1、while循环 PHP 中最简单的循环
//指定的条件为真,while 循环执行代码块 (输出1~9)
$i = 1;
$max = 10;
while($i<$max){
echo $i; echo '<br>'; $i++;
}
2、do...while循环
//先执行一次,后检查条件,若条件为真,则重复循环 (输出1~9)
$i = 1;
$max = 10;
do{
echo $i; echo '<br>'; $i++;
}while($i<$max);
3、for循环 PHP 中最复杂的循环
//若确定循环次数,可使用for循环 (输出1~9)
$i = 1;
$max = 10;
for ($i; $i < $max; $i++) {
echo $i; echo '<br>';
}
4、foreach 只适用于数组,并用于遍历数组中的每个键/值对
foreach($array as $key => $val) {
echo $val;
}
foreach($array as $val) {
echo $val;
}
例子:用for、while、do...while 打印出99乘法表
for ($j = 1; $j <= 9; $j++) {
for ($i = 1; $i <= $j; $i++) { echo "{$i}x{$j}=" . ($i * $j) . " "; } echo "<br />";
}
$j = 1;
while ($j <= 9) {
$i = 1; while ($i <= $j) { echo "{$i}x{$j}=" . ($i * $j) . " "; $i++; } echo "<br />"; $j++;
}
$j = 1;
do {
$i = 1; do { echo "{$i}x{$j}=" . ($i * $j) . " "; $i++; } while ($i <= $j); echo "<br />"; $j++;
} while ($j <= 9);
相关推荐
MXstudying 2020-09-05
WasteLand 2020-09-15
<?php. if (!empty($_POST)) {. $data1 = $_POST["data1"];$data2 = $_POST["data2"];$fuhao = $_POST["fuh
mathchao 2020-09-15
zyyjay 2020-11-09
xuebingnan 2020-11-05
samtrue 2020-11-22
stefan0 2020-11-22
yifangs 2020-10-13
songshijiazuaa 2020-09-24
hebiwtc 2020-09-18
天步 2020-09-17
83911535 2020-11-13
whatsyourname 2020-11-13
zhouyuqi 2020-11-10
Noneyes 2020-11-10
mathchao 2020-10-28
王志龙 2020-10-28