惯例,先上代码
<?php header('content-type:text/html;charset=utf-8'); echo '<hr>'; echo <<< 'INPUT' <form> <h2>乘法表生成</h2> 请输入数字 <input type="text" name="number"> <button>生成</button> </form> INPUT; $num = isset($_GET['number']) ? $_GET['number']:'null'; //echo "$num"; echo '<table border="1" cellpadding="10" cellspacing="0" style="border-collapse:collapse"><h2>利用for循环生成乘法表表格</h2>'; for ($m = 1; $m<=9; $m++){ echo '<tr>'; for ($n = 1;$n<=$m;$n++){ if ($n<=$num){ echo '<td>'; echo "$m X $n = ".$m*$n.""; } echo '</td>'; } } echo "</tr>"; echo "</table>"; echo '<hr>'; echo '<table border="1" cellpadding="10" cellspacing="0" style="border-collapse:collapse"><h2>利用for循环生成乘法表</h2>'; for ($a = 1; $a <= 9; $a++) { echo '<tr>'; for ($b = 1; $b <= $a; $b++) { echo '<td>'; echo "$a*$b=" . $a * $b . ""; echo '</td>'; } } echo "</tr>"; echo "</table>"; echo '<hr>'; echo '<table border="1" cellpadding="10" cellspacing="0" style="border-collapse:collapse"><h2>利用while循环生成乘法表</h2>'; $d = 1; while ($d<=9){ echo '<tr>'; $e = 1; while ($e<=$d){ echo '<td>'; echo "$d*$e=".$d*$e.""; $e++; echo '</td>'; }$d++; } echo "</tr>"; echo "</table>"; echo '<hr>'; echo '<table border="1" cellpadding="10" cellspacing="0" style="border-collapse:collapse"><h2>利用do while循环生成乘法表</h2>'; $x = 1; do { echo '<tr>'; $y = 1; do { echo '<td>'; echo "$x X $y = ".$x*$y.""; $y++; echo '</td>'; }while($y<=$x); $x++; }while($x<=9); echo "</tr>"; echo "</table>";
运行结果
http://lib.ourlib.cc/study1.php
备注
<table border="1" cellpadding="10" cellspacing="0" style="border-collapse:collapse">
border-collapse属性:collapse 将重叠的边框合并,不会让边框显示的很粗,影响整体效果
备注2:while循环
while (条件为真) { 要执行的代码; }
备注3:do while循环 do while 跟while的顺序刚好是相反的
do { 要执行的代码; } while (条件为真);