这个PHP代码有什么问题

时间:2010-11-12 21:44:05

标签: php html

由于某种原因,这个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 个答案:

答案 0 :(得分:6)

呃,你想要数字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();
?>

答案 1 :(得分:3)

问题:$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;

答案 2 :(得分:1)

它正在运作。但是你的标签是错误的,它应该是“数字之和1 - 15 ”。这里:http://codepad.org/TEvtB1hL

答案 3 :(得分:1)

你在这里遇到了问题:

$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;)。

答案 4 :(得分:0)

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;

在5 *之后缺少pow

接下来,看一下

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>