php使用while,do-while,for循环打印99乘法表
2020-07-04 重庆LiSEO工作室
php使用while,do-while,for循环打印99乘法表,源码如下:
<?php //while echo "使用while循环打印1到9的乘法表<br>"; $a=1; while($a<=9){ $b=1; while($b<=$a){ echo "$a X $b = ".($a*$b).' '; $b++; } $a++; echo '<br>'; } //do while echo "使用do while循环打印1到9的乘法表<br>"; $a=1; do{ $b=1; do{ echo "$a X $b = ".($a*$b).' '; $b++; } while($b<=$a); $a++; echo '<br>'; }while($a<=9); //for echo "使用for循环打印1到9的乘法表<br>"; for($a=1;$a<=9;$a++){ for($b=1;$b<=$a;$b++){ echo "$a X $b = ".($a*$b).' '; } echo '<br>'; } ?>
运行结果如下:
使用while循环打印1到9的乘法表 1 X 1 = 1 2 X 1 = 2 2 X 2 = 4 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81 使用do while循环打印1到9的乘法表 1 X 1 = 1 2 X 1 = 2 2 X 2 = 4 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81 使用for循环打印1到9的乘法表 1 X 1 = 1 2 X 1 = 2 2 X 2 = 4 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81