指定数字的Php乘法表

指定数字的Php乘法表,php,loops,for-loop,while-loop,Php,Loops,For Loop,While Loop,我有一个任务,创建一个脚本,将输出乘法表只为指定的数字。要创建常规乘法表,例如10x10,我们将编写如下内容: echo "<table border=\"1\">"; for ($r =0; $r < $rows; $r++){ echo'<tr>'; for ($c = 0; $c < $cols; $c++) echo '<td>' .$c*

我有一个任务,创建一个脚本,将输出乘法表只为指定的数字。要创建常规乘法表,例如10x10,我们将编写如下内容:

echo "<table border=\"1\">";

        for ($r =0; $r < $rows; $r++){

            echo'<tr>';

            for ($c = 0; $c < $cols; $c++)
                echo '<td>' .$c*$r.'</td>';
           echo '</tr>'; // close tr tag here

        }

  echo"</table>";
|1 x 1 = 1|1 x 2 = 2|1 x 3 = 3|
| ------- | ------- | ------- |
|2 x 1 = 2|2 x 2 = 4|2 x 3 = 6|
|3 x 1 = 3|3 x 2 = 6|3 x 3 = 9|

有没有人知道如何使用php(while和/或for)循环来响应这一点?

听起来您想要输出计算文本和结果,例如
1 x 3=3
。您的输出中缺少了这一点


另外,您需要在
1
而不是
0
处启动
for
循环,否则您将得到
0 x 0=0
,我认为您不需要。您可以通过使用
来补偿迭代的损失,难道您不能将它添加到
echo
中吗?一个简单的连接就可以了
echo '<table border="1">';
for ($r = 1; $r <= $rows; $r++) {
    echo '<tr>';
    for ($c = 1; $c <= $cols; $c++) {
        echo sprintf('<td>%d x %d = %d</td>', $r, $c, $c * $r);
    }
    echo '</tr>'; // close tr tag here
}
echo '</table>';
$cellType = ($r === 1) ? 'th' : 'td'; // use <th> for the first row, otherwise <td>
echo sprintf('<%s>%d x %d = %d</%s>', $cellType, $r, $c, $c * $r, $cellType);