In: Computer Science
1. Create a PHP page with standard HTML tags. Remember to save
the file with
the .php extension.
Inside the <body> tag, create a PHP section that will show
the text "Hello
World!"
2. For this exercise, echo the phrase "Twinkle, Twinkle little
star." Create
two variables, one for the word "Twinkle" and one for the word
"star". Echo
the statement tothe browser.
3. PHP includes all the standard arithmetic operators. For this
PHP
exercise, you will use them along with variables to print equations
to the
browser. In your script, create the following variables:
$x=10;
$y=7;
Write code to print out the following:
10 + 7 = 17
10 - 7 = 3
10 * 7 = 70
10 / 7 = 1.4285714285714
10 % 7 = 3
Use numbers only in the above variable assignments, not in the
echo
statements. You will need a third variable as well.
Note: this is intended as a simple, beginning exercise, not using
arrays or
loops.
4. Arithmetic-assignment operators perform an arithmetic operation
on the
variable at the same time as assigning a new value. For this PHP
exercise,
write a script to reproduce the output below. Manipulate only one
variable
using no simple arithmetic operators to produce the values given in
the
statements.
Hint: In the script each statement ends with "Value is now
$variable."
Value is now 8.
Add 2. Value is now 10.
Subtract 4. Value is now 6.
Multiply by 5. Value is now 30.
Divide by 3. Value is now 10.
Increment value by one. Value is now 11.
Decrement value by one. Value is now 10.
Question 1
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
Code
Output
Question 2
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$word1 = "Twinkle";
$word2 = "star";
echo $word1.", ".$word1." little ".$word2;
?>
</body>
</html>
Code
Output
Question 3
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$x = 10;
$y = 7;
$res = $x + $y;
print($x." + ".$y." = ".$res."<br>");
$res = $x - $y;
print($x." - ".$y." = ".$res."<br>");
$res = $x * $y;
print($x." * ".$y." = ".$res."<br>");
$res = $x / $y;
print($x." / ".$y." = ".$res."<br>");
$res = $x % $y;
print($x." % ".$y." = ".$res."<br>");
?>
</body>
</html>
Code
Output
Question 4
<html>
<body>
<?php
$x = 8;
echo "Value is now ".$x."<br>";
$y = 2;
echo "Add ".$y.".";
$x = $x + $y;
echo "Value is now ".$x.".<br>";
$y = 4;
echo "Subtract ".$y.".";
$x = $x - $y;
echo "Value is now ".$x.".<br>";
$y = 5;
echo "Multiply by ".$y.".";
$x = $x * $y;
echo "Value is now ".$x.".<br>";
$y = 3;
echo "Divide by ".$y.".";
$x = $x / $y;
echo "Value is now ".$x.".<br>";
echo "Increment value by one. ";
$x++;
echo "Value is now ".$x.".<br>";
echo "Decrement value by one. ";
$x--;
echo "Value is now ".$x.".<br>";
?>
</body>
</html>
Code
Output