代码之家  ›  专栏  ›  技术社区  ›  Casey Flynn

这个php代码有什么问题

  •  0
  • Casey Flynn  · 技术社区  · 14 年前

    由于某些原因,此php代码不会回显。我好像不明白为什么。

    <?php
    function prob1(){
        $sum1 = 0;
        for($i=1; $i<11; $i++){
            $sum1 = $sum1 + $i; 
        }
        echo "Sum of numbers 1 - 10 is:". $sum1;
    }
    prob1();
    ?>
    

    有什么想法吗?

    UPDATE: Here is the entire web page that it exists in.
    <html>
    <head><title>Lab 13</title></head>
    <body>
    
    <h1>Problem 1:</h1>
    
    <?php
    function prob1(){
        $sum1 = 0;
        for($i=1; $i<11; $i++){
            $sum1 = $sum1 + $i; 
        }
        echo "Sum of numbers 1 - 10 is:". $sum1;
    }
    prob1();
    ?>
    
    <p>
    A. This is a server-side program. The code is executed on a web server and the results are returned to the user as HTML.
    <br />
    B. You can use javascript to accomplish the same calculation. Javascript code is executed client-side; meaning the user's machine will execute the code and return the result.
    <br />
    C. There is no need to use PHP in a scenario like this. Calculations such as these are more efficiently executed client-side.
    <br />
    </p>
    
    <h1>Problem 2</h1>
    
    <?php
    
    function prob2($x){
    $y = 0;
    
    if ($x<5)
    {
        $y = pow($x, 3) + 5*($x, 2) + 3;
    }
    else
    {
        $y = pow($x, 3) - 9*pow($x, 2) + 12;
    }   
    
    echo "For X=" . $x . " Y=" + $y;
    }
    
    prob2(3);
    prob2(8);
    
    ?>
    
    <p>A. Yes, you can write the same code using javascript. In this situation, it wouldn't be necessary to use php unless you wanted to hide the calculation from the user.</p>
    
    </body>
    </html>
    
    5 回复  |  直到 14 年前
        1
  •  6
  •   Ed Manet    14 年前

    嗯,你想要1到10的和,但是你循环了16次。

        <?php
         function prob1(){
           $sum1 = 0;
           for($i=1; $i<=10; $i++){
              $sum1 += $i; 
           }
           echo "Sum of numbers 1 - 10 is:". $sum1;
         }
         prob1();
    ?>
    
        2
  •  3
  •   ajreal    14 年前

    问题: $i<16 = Sum of numbers 1 - 10 ?

    <? function prob1() { echo "Sum of numbers 1 - 10 is ", array_sum(range(1,10)); } prob1();

    更新

    $y = pow($x, 3) + 5*($x, 2) + 3;
    

    应该是

    $y = pow($x, 3) + 5*pow($x, 2) + 3;
    
        3
  •  1
  •   Ruel    14 年前

    它起作用了。但你的标签错了,应该是 数字和1-15 ". 在这里: http://codepad.org/TEvtB1hL

        4
  •  1
  •   Mike C    14 年前

    你有个问题:

    $y = pow($x, 3) + 5*($x, 2) + 3;
    

    这条线应该是

    $y = pow($x, 3) + 5*pow($x, 2) + 3;
    

    在我修改之后,页面正常加载。

    (同时 echo "For X=" . $x . " Y=" + $y; 应该是 echo "For X=" . $x . " Y=" . $y; ).

        5
  •  0
  •   JAL    14 年前

    PHP在服务器上运行不正常,或者它被保存为.htm文件,而不是.PHP。

    执行此操作时,会出现一些解析错误。他们是:

    Parse error: syntax error, unexpected ',' in /var/duck/dork.php on line 36
    

    第36行是

    $y = pow($x, 3) + 5*($x, 2) + 3;
    

    它缺少一个 pow 5*之后。

    接下来,看看这条线

    echo "For X=" . $x . " Y=" + $y;
    

    “+”应该是“.”,以便将$y连接到该字符串。更好的方法是,使用双引号作为模板。。并添加 <BR> 所以输出不在同一行上。

    echo "For X=$x Y=$y <br>";
    

    这是完整的工作代码。

    <html>
    <head><title>Lab 13</title></head>
    <body>
    
    <h1>Problem 1:</h1>
    
    <?php
    function prob1(){
        $sum1 = 0;
        for($i=1; $i<11; $i++){
            $sum1 = $sum1 + $i; 
        }
        echo "Sum of numbers 1 - 10 is: $sum1";
    }
    prob1();
    ?>
    
    <p>
    A. This is a server-side program. The code is executed on a web server and the results are returned to the user as HTML.
    <br />
    B. You can use javascript to accomplish the same calculation. Javascript code is executed client-side; meaning the user's machine will execute the code and return the result.
    <br />
    C. There is no need to use PHP in a scenario like this. Calculations such as these are more efficiently executed client-side.
    <br />
    </p>
    
    <h1>Problem 2</h1>
    
    <?php
    
    function prob2($x){
    $y = 0;
    
    if ($x<5)
    {
        $y = pow($x, 3) + 5*pow($x, 2) + 3;
    }
    else
    {
        $y = pow($x, 3) - 9*pow($x, 2) + 12;
    }   
    
    echo "For X=$x Y=$y";
    echo "<br>";
    }
    
    prob2(3);
    prob2(8);
    
    ?>
    
    <p>A. Yes, you can write the same code using javascript. In this situation, it wouldn't be necessary to use php unless you wanted to hide the calculation from the user.</p>
    
    </body>
    </html>