输入数字并使用 php 表格打印结果


Input numbers and print results using a table in php

我正在为期末考试而学习,我遇到了这个问题:

write a PHP script which reads a positive integer
and displays the sum, the number, sum N*N and N!
for example n=6 will display sum= 1,3,6,10,15,21
and N*N = 1,4,9,16,25,36
N!=1,2,6,24,120,720.
我已经设法做到了数字,我已经

研究了,您可以使用内置的阶乘和求和方法,我已经尝试过,但是当我输出它时我得到空白页。

这是我到目前为止的代码:

<html>
   <body>
      <form action="values.php" method="post" >
         num:<input type="text" name="num" size ="5"/>
         <input type = "submit" value = "Submit number" />
      <table border = "2">
        <th> Number </th>
        <th> Sum </th>
        <th> N*N </th>
        <th> N! </th>
        </tr>
     <?php
        $num=$_POST["num"];
        if ($num==0)
            $num="";
        else
        {
           $sum=0;
           for($i=0; $i<=$num; $i++){
           $sum=$sum+$i;
        }
     }

          for ($number = 1; $number <=6; $number++)
          {
            $total=0;
            $num=(int)$_POST['num'];
            $total=$total+$num;
               $root = sqrt($number);
               $sum =($number*$total);
               $ntn =($number*($total*$total));
               $fact =($number-1);
               print("
               <tr align = 'center'>
               <td> $number </td>
               <td> $sum </td>
               <td>$ntn </td>
               <td>$fact</td>
               </tr>'n");
          }
    ?>
</table>

</body>

任何帮助将不胜感激。

提前感谢!

工作代码:

你似乎没有理解这个问题。"sum"列是"所有数字的总和,直到i",其中i的范围从0到$num。

N*N 是保持"i 的平方",最后一个保持"i 的阶乘"。

<html>
   <body>
      <form action="values.php" method="post" >
         num:<input type="text" name="num" size ="5"/>
         <input type = "submit" value = "Submit number" />
      <table border = "2">
        <th> Number </th>
        <th> Sum </th>
        <th> N*N </th>
        <th> N! </th>
        </tr>
     <?php
        $num=$_POST["num"];
        if ($num==0){
            $num="";
     }
$sum=0;
$fact=1;
          for ($number = 1; $number <=$num; $number++)
          {
           $sum=$sum+$number;
               $ntn =$number*$number;
               $fact =$number*$fact;
               print("
               <tr align = 'center'>
               <td> $number </td>
               <td> $sum </td>
               <td>$ntn </td>
               <td>$fact</td>
               </tr>'n");
          }
    ?>
</table>

</body>