PHP 中的函数内部循环

如何编写一个 php 程序来使用 2 个函数显示此输出

我知道这段代码是错误的,但它看起来应该与此接近,函数让我感到困惑


<body>


<table border="1">

<tr><th>i</th><th>square</th><th>cube</th></tr>



<?php 


function square($x)


{

return $x * $x ;


}

function cube($y)

{

        return $y * $y * $y ;

}

for ($i=1; $i <= 10 ; $i++) 

    echo "


<tr>

    <td>$i</td>

    <td>square('$i');</td>

    <td>cube('$i');</td>


</tr>";



 ?>


</table>

</body>


小唯快跑啊
浏览 132回答 3
3回答

明月笑刀无情

函数调用未正确连接:<style>&nbsp; &nbsp; &nbsp;table {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; border-collapse: collapse;&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;table, th, td {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; border: 1px solid black;&nbsp; &nbsp; &nbsp;}</style><body><table style="border: 1">&nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>i</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>square</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>cube</th>&nbsp; &nbsp; &nbsp;</tr>&nbsp; &nbsp; &nbsp;<?php&nbsp;&nbsp; &nbsp; &nbsp;function square($x){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $x * $x ;&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;function cube($y){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $y * $y * $y ;&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;for ($i=1; $i <= 10 ; $i++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>$i</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>".square($i)."</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>".cube($i)."</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>";&nbsp; &nbsp; &nbsp;?></table></body>

炎炎设计

我建议进行一些其他更改。关注点分离:更少的代码行并不一定意味着它更简单或更容易维护。一口大小的块和其中较少的决策分支可以做到这一点。从最基本的形式来看,该原则要求您不要将算法逻辑与表示逻辑混合在一起。view.php<style>     table {          border-collapse: collapse;     }     table, th, td {          border: 1px solid black;     }</style><body><table style="border: 1">     <tr>          <th>i</th>          <th>square</th>          <th>cube</th>     </tr>     <?php      foreach ($powers as $index => $power) {          echo "<tr><td>$index</td>";          foreach ($power as $value) {               echo "<td>$value</td>";          }          echo "</tr>";     }     ?></table></body>exponentHelpers.php    function square($x)    {        return $x * $x ;    }    function cube($y)    {        return $y * $y * $y ;    }controller.phprequire_once "exponentHelpers.php";$base = 10;$powers = [];while($base--) {    //Note, the self-decrementing short-hand will result in the values listed in reverse order.                    //You could write it long-hand if you prefer, or call array_reverse() afterwards.    $powers[] = [        square($base),        cube($base),    ];}require_once "view.php";PSR2:学习好习惯永远不嫌早。PSR2 基本上描述了一种格式化代码的行业标准,以使其看起来一致且易于阅读,无论是谁编写的。你那里有一些违规行为。制表符应该是 4 个空格,而不是 5 个。函数的左括号应另起一行。避免单行控制结构。即,对循环和 if 语句使用括号:for ($i=1; $i <= 10 ; $i++) {    echo "        <tr>            <td>$i</td>            <td>".square($i)."</td>            <td>".cube($i)."</td>        </tr>";}幂函数:你用square()和cube()函数重新发明了轮子。Php 提供了pow($base, $exponent)执行相同操作的函数,并且不限于一种幂。所以这可以完全消除该exponentHelpers.php部分。符合 PSR2 的简写:如果您想使用它,这完全是您的偏好,但是您可能会对此处的 Php 的两个部分感兴趣,请查看 参考资料 部分中的循环view.php。一种是array_map(),它允许在同一行上进行命令式数组循环和结果检索。另一个是<?=,它是 的 HTML 模板简写<?php echo ...。将它们放在一起,您可以更简洁地展示您的循环:<?= array_map(function (array $power, $index) {    //Newlines are optional. Just makes the output a little easier to read.    $powerElms = preg_replace("/^.*$/", "<td>$0</td>", $power);    return "<tr>\n<td>$index</td>\n" . implode("\n", $powerElms) . "</tr>\n";}, $powers, array_keys($powers)) //Missing semicolon is not a typo. It's not needed with this syntax. ?>

慕运维8079593

你已经非常接近你想要的了。您应该更改“for”循环,以便您的代码最终看起来像以下几行:<table border="1">&nbsp; &nbsp; <tr><th>i</th><th>square</th><th>cube</th></tr>&nbsp; &nbsp; <?php&nbsp; &nbsp; function square($x) {&nbsp; &nbsp; &nbsp; &nbsp; return $x * $x ;&nbsp; &nbsp; }&nbsp; &nbsp; function cube($y) {&nbsp; &nbsp; &nbsp; &nbsp; return $y * $y * $y ;&nbsp; &nbsp; }&nbsp; &nbsp; for ($i=1; $i <= 10 ; $i++){&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?php echo $i; ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?php echo square($i); ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?php echo cube($i); ?></td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; <?php } ?></table>
打开App,查看更多内容
随时随地看视频慕课网APP