For some reason this php code won't echo. 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.
<?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
?
<? 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 ". Here: 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;
).
Either PHP is not functioning properly on the server, or this is saved as an .htm file, not .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
$y = pow($x, 3) + 5*($x, 2) + 3;
It is missing a pow
after the 5*.
Next, take a look at the line
echo "For X=" . $x . " Y=" + $y;
The + should be a '.' to concatenate $y to that string. 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.
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.