微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在PHP中循环内部函数

如何解决在PHP中循环内部函数

How do I write a php program that will show this output using 2 functions

我知道这段代码错误的,但它看起来应该与此相似,函数使我困惑

<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>

解决方法

函数调用未正确连接:

<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 

     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>
,

您非常接近想要的东西。您应该更改“ for”循环,以使您的代码最终看起来像以下几行:

<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++){
        ?>
        <tr>
            <td><?php echo $i; ?></td>
            <td><?php echo square($i); ?></td>
            <td><?php echo cube($i); ?></td>
        </tr>
    <?php } ?>
</table>
,

欢迎使用StackOverflow。 Majed的答案是正确的,应该将其标记为已接受的答案。不过,我建议您进行其他一些更改。

  1. Separation of Concern更少的代码行并不一定意味着它更容易维护。可以做到这一点。在最基本的形式中,该原理要求您不要将算法逻辑与表示逻辑混在一起。

view.php

<style>
     table {
          border-collapse: collapse;
     }

     table,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.php

require_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";
  1. 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>";
}
  1. Power function:您通过square()cube()函数重新发明了轮子。 Php提供了一种pow($base,$exponent)函数,它执行相同的功能,并且不限于一种功效。因此,这可以完全消除exponentHelpers.php部分。

  2. 符合PSR2的速记:如果您想使用它,则完全是您的首选项,但是在view.php部分中的循环中,您可能会感兴趣有两点Php。一个是array_map(),它允许imperative数组循环和在同一行上检索结果。另一个是<?=,它是<?php echo ...的HTML模板的缩写。将它们放在一起,您可以更简洁地呈现循环:

<?= 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. ?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。