简体   繁体   English

这个PHP代码有什么问题

[英]What's wrong with this php code

For some reason this php code won't echo. 由于某种原因,此php代码不会回显。 I can't seem to figure out why. 我似乎不知道为什么。

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

Any ideas? 有任何想法吗?

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>

Uh, you want the sum of the number 1 thru 10 but you're looping 16 times. 嗯,您想要数字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();
?>

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

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

updated 更新

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

should be 应该

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

It's working. 工作正常 But your label is wrong though, it should be " Sum of numbers 1 - 15 ". 但是您的标签是错误的,应该是“ 数字1-15的总和 ”。 Here: http://codepad.org/TEvtB1hL 此处: http//codepad.org/TEvtB1hL

You've got a problem here: 您在这里遇到问题:

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

This line should be 这行应该是

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

After I changed that, the page loaded for me normally. 更改后,页面正常为我加载。

(Also echo "For X=" . $x . " Y=" + $y; should be echo "For X=" . $x . " Y=" . $y; ). (还echo "For X=" . $x . " Y=" + $y;应该echo "For X=" . $x . " Y=" . $y; )。

Either PHP is not functioning properly on the server, or this is saved as an .htm file, not .php. PHP可能在服务器上无法正常运行,或者被另存为.htm文件而不是.php。

When this is executed, you get a few parse errors. 执行此操作时,您会遇到一些解析错误。 They are: 他们是:

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

line 36 is 第36行是

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

It is missing a pow after the 5*. 5 *之后缺少pow

Next, take a look at the line 接下来,看看线

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

The + should be a '.' +应该是一个'。 to concatenate $y to that string. 将$ y连接到该字符串。 Even better, use double quotes for the templating purpose they are intended for.. and add a <BR> so the output is not on the same line. 更好的是,将双引号用于其预期的模板用途。并添加<BR>以使输出不在同一行上。

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

Here's the complete, working code. 这是完整的工作代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM