2014-07-25 60 views
-1

如何在表格中打印函數的值?我很難弄清楚這一點,在Google中查看它,觀看一些Youtube PHP教程,但仍然沒有任何結果,還有一個錯誤,告訴我$ cols,$ rows,$ rNumber未定義。輸出應該看起來像一個半金字塔。在表格中打印函數值?

<?php 
do { 
    $rNumber = mt_rand(3, 11); 
} while ($rNumber % 2 == 0); 

echo '<strong>Looping Statements Exercises</strong>'; 
echo '<br>'; 
echo '<br>'; 
echo 'This page shows eight (8) figures programatically generated by looping statements. Number of rows are random odd integer between 3 and 11.'; 
echo '<br>'; 
echo '<br>'; 


echo '<table border = "1">'; 
echo '<tr>'; 
echo '<td colspan = "4"><center>Size:<strong>', $rNumber, ' x ', $rNumber, '</strong>'; 
echo '</tr>'; 
echo '<tr>'; 
echo '<td> Figure A'; 
echo '<td> Figure B'; 
echo '<td> Figure C'; 
echo '<td> Figure D'; 
echo '</tr>'; 
echo '<tr>'; 
echo '<td> Figure A here'; 
echo '<td>', figure_b($cols, $rows), '</td>'; 
echo '<td> Figure C here'; 
echo '<td> Figure D here'; 
echo '</tr>'; 
echo '<tr>'; 
echo '<td> Figure E'; 
echo '<td> Figure F'; 
echo '<td> Figure G'; 
echo '<td> Figure H'; 
echo '</tr>'; 
echo '<tr>'; 
echo '<td> Figure E here'; 
echo '<td> Figure F here'; 
echo '<td> Figure G here'; 
echo '<td> Figure H here'; 
echo '</tr>'; 


function figure_b($rows, $cols) 
{ 
    if ($rNumber == 5) { 
     for ($rows = 0; $rows <= 5; $rows++) { 
      for ($cols = 0; $cols <= 5; $cols++) { 

       return $cols; 
      } 
      return $rows; 
     } 
    } 
} 

echo '</table>';     
+0

figure_b($的cols,$行)在哪裏那個變量? –

+0

這不是導致錯誤,但也許你應該關閉​​標籤... – lpg

回答

-1
<?php 

do{ 
             $rNumber = mt_rand(3,11); 
             }while ($rNumber % 2 == 0); 

             echo '<strong>Looping Statements Exercises</strong>'; 
             echo '<br>'; 
             echo '<br>'; 
             echo 'This page shows eight (8) figures programatically generated by looping statements. Number of rows are random odd integer between 3 and 11.'; 
             echo '<br>'; 
             echo '<br>'; 


             echo '<table border = "1">'; 
             echo '<tr>'; 
             echo '<td colspan = "4"><center>Size:<strong>',$rNumber,' x ',$rNumber,'</strong>'; 
             echo '</tr>'; 
             echo '<tr>'; 
             echo '<td> Figure A'; 
             echo '<td> Figure B'; 
             echo '<td> Figure C'; 
             echo '<td> Figure D'; 
             echo '</tr>'; 
             echo '<tr>'; 
             echo '<td> Figure A here'; 
             echo '<td>', figure_b(5, 5,$rNumber),'</td>'; 
             echo '<td> Figure C here'; 
             echo '<td> Figure D here'; 
             echo '</tr>'; 
             echo '<tr>'; 
             echo '<td> Figure E'; 
             echo '<td> Figure F'; 
             echo '<td> Figure G'; 
             echo '<td> Figure H'; 
             echo '</tr>'; 
             echo '<tr>'; 
             echo '<td> Figure E here'; 
             echo '<td> Figure F here'; 
             echo '<td> Figure G here'; 
             echo '<td> Figure H here'; 
             echo '</tr>'; 


             function figure_b($rows, $cols,$rNumber) 
             { 
              if($rNumber == 5) 
              { 
               for($rows = 0; $rows<=5; $rows++) 
               { 
                for ($cols = 0; $cols<=5; $cols++) 
                { 

                return $cols; 
                } 
               return $rows; 
               } 
              } 
             } 
             echo '</table>'; 

?> 
0

您的功能:

function figure_b($rows, $cols) 
{ 
if($rNumber == 5) 
{ 
for($rows = 0; $rows<=5; $rows++) 
{ 
for ($cols = 0; $cols<=5; $cols++) 
{ 

return $cols; 
} 
return $rows; 
} 
} 
} 

是完全錯誤的。

它應該是相當:

function figure_b($rNumber) 
{ 
    if ($rNumber == 5) { 
     for ($rows = 0; $rows <= 5; $rows++) { 
      for ($cols = 0; $cols <= 5; $cols++) { 
       echo $cols.' '; 
      } 
      echo $rows.'<br />'; 
     } 
    } 
} 

如果你想你的表內顯示任何內容,並使用rNumber變量(似乎沒有必要通過這裏$rows$cols變量)

但是你的代碼是非常糟糕的,你不應該這樣混合使用PHP代碼和HTML代碼。如果您需要將代碼的某些部分移到其他文件中,而不是使用太多的echo,因爲它也會影響您的性能。

0

你可以通過嘗試昌河這

$rNumber = mt_rand(3,11); 
}while ($rNumber % 2 == 0); 

這個開始:

$rNumber = mt_rand(3,11);        
    while ($rNumber % 2 == 0); 

和你的函數:

function figure_b($rNumber) 
    { 
       if($rNumber == 5) 
       { 
        for($rows = 0; $rows<=5; $rows++) 
         { 
          for ($cols = 0; $cols<=5; $cols++) 
           { 

            echo $cols; 
           } 
            echo $rows; 
          } 
        } 
}