Sunteți pe pagina 1din 8

Question 1

<?php
print("Hello, world. <BR>");
print("PHP is neato. <BR>");
print("4+8 <BR>");
print(4+8);
print("<BR>");
$total = 4+8;
print("$total <BR>");
?>

Compare the web browser view line by line with the PHP source code. Answer the following questions:

1) What does the PHP print statement do?


2) What is the difference between print("4+8") and print(4+8)?

View in http://localhost/xampp/lisa/lab_1.php

("4+8")
(4+8)
Question 2

form.php

<html>
<head>
</head>
<body>
This script demonstrates passing values within an array.
<p>
<form method="post" action="process.php">
<?php

$labels = array("First", "Second", "Third");


while (list($index, $name) = each($labels))
echo "$name number:&nbsp;<input
type=text name='vals[]'><p>\n";
?>

<input type=submit>
</form>
</body>
</html>

process.php

<html>
<head>
</head>
<body>
This script demonstrates using values which
were passed in an array.
<p>
<?php
$vals = $HTTP_POST_VARS["vals"];
$sum = 0;
for ($i = 0; $i < 3; ++$i)
{
echo "Value $i: $vals[$i].<br>\n";
$sum += $vals[$i];
}
echo "<p>The sum of those three numbers is $sum.\n";
?>
</body>
</html>

The first PHP script should generate a form which allows the user to input 10 numbers.

1) When the forms submit button is pressed, the 10 numbers should be sent to the second PHP
script as an array.

New coding for display 10 numbers

<html>
<head>
</head>
<body>
<center>
<body background="cute_girl1.jpg" style="background-attachment:fixed ; background-position:center ;
color:#0099FF; font-family:Caduceus ; margin-top:100px">
This script demonstrates passing values within an array.
<p>
<form method="post" action="process.php">

<?php
$labels = array
("First", // This will ask user to enter first number
"Second", // This will ask user to enter second number
"Third" , // This will ask user to enter third number
"Fourth" , // This will ask user to enter fourth number
"Fifth" , // This will ask user to enter fifth number
"Sixth" , // This will ask user to enter sixth number
"Seventh" , // This will ask user to enter seventh number
"Eighth" , // This will ask user to enter eighth number
"Nineth" , // This will ask user to enter nineth number
"Tenth"); // This will ask user to enter tenth number

while (list($index, $name) = each($labels))


echo "$name number:&nbsp;
<input type=text name='vals[]'><p>\n";
?>

<input type=submit style="background-color:#0099FF">


</form>
</body>
</html>
View in http://localhost/xampp/lisa/form.php
2) The second script should display the sum in the array and the average of the 10 numbers.
3) The second script also display the largest number and the smallest number

New coding for display the largest number and the average of the 10 numbers

<html>
<head>
</head>
<body>
<center>
<body background="cute_girl1.jpg" style="background-attachment:fixed ; background-position:center ;
color:#0099FF; font-family:Copyshop ; margin-top:100px ; margin-right:120px">
This script demonstrates using values which were passed in an array.
<p>
<?php
$vals = $HTTP_POST_VARS["vals"]; // declaration value that enter by the user
$sum = 0; // declaration total sum of the value
$max = $vals[0]; // declaration for the largest value that enter by the user
$min = $vals[0]; // declaration for the smallest value that enter by the user

for ($i = 0; $i < 10; ++$i)


{
if($vals[$i] > $max)
{
$max = $vals[$i];
}
if($vals[$i] < $min)
{
$min = $vals[$i];
}

echo "Value $i: $vals[$i].<br>\n";


$sum += $vals[$i]; // calculate total sum
$avg = $sum/10; // calculate average

}
// Display output
echo "<p>The sum of those 10 numbers is $sum.\n";
echo "<p>The average of those 10 numbers is $avg.\n";
echo "<p>The largest of those 10 numbers is $max.\n";
echo "<p>The smallest of those 10 numbers is $min.\n";

?>
</body>
</html>
View in http://localhost/xampp/lisa/process.php

S-ar putea să vă placă și