Web App. Dev. Security - Lab Manual 5-2 - PHP-2
Web App. Dev. Security - Lab Manual 5-2 - PHP-2
After completing this lab exercises, the student will be able to:
Needed Materials:
PC
XAMPP (Apache)
Notepad++
Course Outcomes:
Describe how client-side and server-side programs enhance the functionality of the web site.
Use language objects, functions and programming constructs to control the program flow
PHP Intro
PHP Syntax
PHP Variables
PHP Echo
PHP Operators
PHP If..Else..Elseif
PHP Switch
PHP Intro:
<?php
$x=10;
$myname = "Ahmad";
echo $myname . " is my name";
?>
Practice:
| Page 2
1. Write a PHP code that will manipulate two numbers. Compute and output the sum,
difference and the square of the first number.
<?php
$x=10;
$y=20;
$sum = $x + $y;
$diff = $x - $y;
$sq = $x * $x;
echo "The sum is " . $sum . "<br>";
echo "The difference is " . $diff . "<br>";
echo "The square of the first number is " . $sq;
?>
2. Write PHP code that will manipulate any temperature in Celsius. Then, print its
equivalent Fahrenheit temperature.
F = C * 1.8 + 32
PHP If else
<?php
$average = (70+90+70+80)/4;
if ($average > 70)
echo "Good Job";
else
echo "Study harder";
PHP Switch
<?php
$favcolor="red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, or green!";
| Page 3
}
?>
Laboratory Exercise:
1. Write a PHP code that will manipulate the number of pieces and the amount of an item
purchased by the customer. Assume any value of your choice. Compute and output the Total
Cost.
Example:
The number of pieces:
The amount of an item:
The total cost:
<?php
$P = $_POST['pieces'];
$I = $_POST['item'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Task1</title>
</head>
<body>
<fieldset>
<legend>
Enter Your Purchasing Info
</legend>
| Page 4
<form action="task1.php" method="POST">
The number of pieces: <input type="number" name="pieces"><br>
The amount of an item: <input type="number" name="item"><br><br>
<input type="submit" name="submit"><br>
</form>
<?php
echo "The number of pieces:".$P."<br>";
echo "The amount of an item:".$I."<br>";
echo "The total cost:".($P*$I)."<br>";
?>
</fieldset>
</body>
</html>
2. Write a PHP code that will manipulate Midterm and Final Mark. Assume any value of your
choice. Compute and output the Total Marks by adding the 40% of Midterm and 60% of Final
Marks.
<?php
$M = $_POST['mid'];
$F = $_POST['final'];
$Total = ($M*0.4)+($F*0.6);
?>
<!DOCTYPE html>
<html>
<head>
<title>Task2</title>
</head>
<body>
<fieldset>
<legend>
Enter Your Marks Info
</legend>
<form action="task2.php" method="POST">
| Page 5
Your MidTerm Mark: <input type="number" name="mid"><br>
Your Final Mark: <input type="number" name="final"><br><br>
<input type="submit" name="submit"><br>
</form>
<?php
echo "Your MidTerm Mark: ".$M."<br>";
echo "Your Final Mark: ".$F."<br>";
echo "Your Total Mark is: ".$Total."<br>";
?>
</fieldset>
</body>
</html>
3. ABC Company decided to increase the salary of their employees based on the number of years
in service. Write a PHP code that will assume the current salary of an employee. Compute and
output the new salary from the increased given by the company.
<?php
$Y = $_POST['years'];
$S = $_POST['salary'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Task3</title>
</head>
<body>
| Page 6
<fieldset>
<legend>
Enter Your Info
</legend>
<form action="task1.php" method="POST">
Enter your salary: <input type="number" name="salary"><br>
Enter the amount of years: <input type="number" name="years"><br><br>
<input type="submit" name="submit"><br>
</form>
</fieldset>
</body>
</html>
3. Write PHP code that will manipulate any temperature in Celsius. Then, print its
equivalent Fahrenheit temperature.
F = C * 1.8 + 32
<!DOCTYPE html>
<html>
<head>
<title>Task4</title>
</head>
<body>
</tr>
<tr>
<td> Celsius </td>
<td><input type="number" name="Celsius"></td>
| Page 7
</tr>
</table>
<?php
$Feh = $_POST['Fehrenheit'];
$Cel = $_POST['Celsius'];
?>
</form>
</body>
</html>
| Page 8