亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP 中的函數內部循環

PHP 中的函數內部循環

PHP
小唯快跑啊 2023-10-22 22:06:34
如何編寫一個 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>
查看完整描述

3 回答

?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

函數調用未正確連接:


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


查看完整回答
反對 回復 2023-10-22
?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

我建議進行一些其他更改。

  1. 關注點分離:更少的代碼行并不一定意味著它更簡單或更容易維護。一口大小的塊和其中較少的決策分支可以做到這一點。從最基本的形式來看,該原則要求您不要將算法邏輯與表示邏輯混合在一起。

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.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";

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



查看完整回答
反對 回復 2023-10-22
?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

你已經非常接近你想要的了。您應該更改“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>


查看完整回答
反對 回復 2023-10-22
  • 3 回答
  • 0 關注
  • 178 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號