Laboratory Exercise 3. PHP
Laboratory Exercise 3. PHP
1
I. INTENDED LEARNING OUTCOMES
II. ACTIVITY
2
Code #1: <HTML>
<HEAD>
<TITLE>Converter</TITLE>
</HEAD>
<BODY style="background-color:powderblue;">
<p style="font-family: verdana;">
<div align="center">
<?php
ini_set('display_errors',0);
$fah ==0;
?>
<form action="" method="post">
<?php
ini_set('display_errors',0);
3
if(isset($_POST['compute'])){
$cel = $_POST['cel'];
$Fahrenheit = (float)(($cel * 9 / 5) + 32);// celsius to fahrenheit
}
if(isset($_POST['submit'])){
$fah = $_POST['fah'];
//$celsius = (float)(($fah * 9 / 5) - 32);//fahrenheit to celsius
$div = 5/9;
$div1 = $fah - 32;
$div2 = $div * $div1;
$celsius = (float)($div2);
}
?>
<h1>
<?php
if(isset($_POST['compute'])){
echo "Results for Fahrenheit: ";
echo $Fahrenheit;
}
?>
</h1>
<h1>
<?php
if(isset($_POST['submit'])){
echo "Results for celsius:";
echo $celsius;
}
s ?>
</h1>
</div>
</form>
</BODY>
</HTML>
4
2. The basic rate of a bus is 10php for the first 1km and an additional 2php for every
succeeding 2kms. Input the distance the commuter will travel (in m), and the payment.
Display the total distance travel (in km), the cost of the travel, and the change of the
commuter.
<?php
if (isset($_POST['submit2'])) {
$distance = $_POST['distance'];
5
$payment = $_POST['payment'];
$distancetoKM = $distance/1000;
$minpayment = 10;
$totalfare = 0;
if ($distancetoKM > 1){
if ((distancetoKM)% 1 == 0) {
$change = ($distancetoKM)/2;
$totalfare = ($change*2) + $minpayment;
}
} else {
$totalfare=$minpayment;
}
}
?>
</BODY>
</div>
</HTML>
3. Create a PHP script that will compute and display the area of triangle, square, and
quadratic equations.
6
Code #3: <HTML>
<HEAD>
<TITLE> COMPUTE </TITLE>
</HEAD>
<style>
h2 {
font-family: verdana;
}
</style>
<BODY style="background-color:powderblue;">
<div align="center";>
<form method="POST">
<h2> Find the Area of Triangle </h2>
Base:
<input type="number" name="base" size=30 maxlength=30
placeholder="Input Length of Base"><br><br>
Height:
<input type="number" name="height" size=30 maxlength=30
placeholder="Input Length of Height"><br><br>
<input type="submit" name="submit1" value="Calculate">
</form>
7
<?php
if (isset($_POST['submit1'])) {
$base = $_POST['base'];
$height = $_POST['height'];
$area = ($base*$height)/2;
echo "<b> Answer: </b><br>";
echo "The area of a triangle with base as $base and height as
$height is $area";
?>
<br>
<form method="POST">
<h2> Find the Area of Square </h2>
<input type="number" name="num1" size=30 maxlength=30
placeholder="Input Length of Square side"><br><br>
<input type="submit" name="submit2" size=30 maxlength=30
value="Calculate">
</form>
<?php
if (isset($_POST['submit2'])) {
$a = $_POST['num1'];
$area = $a * $a;
echo "<b> Answer: </b><br>";
echo "The area of a square with the length of " .$a. " is "." "
.$area;
return 0;
}
?>
</BODY
</div>
</HTML>
8
Code #3.2: <HTML>
<HEAD>
<TITLE> QUAD EQUATION </TITLE>
</HEAD>
<style>
{
font-family: times new roman;
}
</style>
<div align="center";>
<form method="POST">
<h2> Find Solution for Quadratic Equation </h2>
A = <input type="text" name="A" size=30 maxlength=30
placeholder="Coefficient A"><br><br>
B = <input type="text" name="B" size=30 maxlength=30
placeholder="Coefficient B"><br><br>
C = <input type="text" name="C" size=30 maxlength=30
placeholder="Coefficient C"><br><br>
<input type="submit" name="submit" value="Find X!">
</form>
9
<?php
if (isset($_POST['submit'])) {
$a = $_POST['A'];
$b = $_POST['B'];
$c = $_POST['C'];
$precision = 4;
$d = $b - 4 * $a * $c;
if($d <0) {
echo "Roots are complex numbers.";
echo "<br>";
else{
echo "Roots are real numbers.";
$root1 = ( -$b + sqrt($d)) / (2* $a);
$root2 = ( -$b - sqrt($d)) / (2* $a);
echo "Roots of quadratic equation are: " .$root1
.$root2;
}
return 0;
?>
10
</BODY>
</div>
</HTML>
III. CONCLUSION:
Our conclusion is what we’ve learned in this topic, basically PHP Numbers can
used in different kind of conversion like Fahrenheit to Celsius and vice versa,
rate within the distance, area of triangle, square, quadratic equation. This
activity gives us an idea on how to build a conversion using PHP Numbers
which we only see in some websites or application that related to these
conversions and equation.
IV. REFERENCE/s
RUBRICS:
11