LAB EVALUATION 2 PHP
LAB EVALUATION 2 PHP
Explanation:
This PHP code defines an associative array of people and their ages, then
sorts the array by age using assort(). It maintains key-value
associations and prints each name with the corresponding age in
ascending order of age using a foreach loop.
Output is:
b) ascending order sort by key
This is the code:
<?php
$people = array("Sophia" => "31", "Jacob" => "41", "William" =>
"39", "Ramesh" => "40");
ksort($people);
foreach ($people as $name => $age) {
echo $name . " => " . $age . "\n";
}
?>
Explanation:
This PHP code creates an associative array of people and their ages, then
sorts the array by names (keys) using ksort(). It maintains the key-value
relationships and uses a foreach loop to print each name and
corresponding age in ascending order of names.
Output:
c) descending order sorting by value
This is code:
<?php
$array = array("Sophia" => "31","Jacob" => "41","William" =>
"39","Ramesh" => "40");
arsort($array);
foreach ($array as $key => $value) {
echo "$key => $value\n";
}
?>
Explanation:
This PHP code creates an associative array of people and their ages, then
sorts the array by ages (values) in descending order using arsort(). It
maintains the association between names and ages, and a foreach loop
prints each name and corresponding age in this sorted order.
Output:
Explanation:
This PHP code creates an associative array of names and ages, then sorts
the array by names (keys) in descending order using krsort(). It
preserves the key-value pairs and uses a foreach loop to print each
name and its corresponding age in descending alphabetical order of
names.
Output:
Q2)
You are tasked with developing a PHP program that calculates electricity bills for
households based on their monthly power consumption. The billing structure is
as follows:
For the first 50 units, the rate is Rs. 3.50 per unit.
For the next 100 units (51 to 150), the rate is Rs. 4.00 per unit.
For the subsequent 100 units (151 to 250), the rate is Rs. 5.20 per unit.
For units exceeding 250, the rate is Rs. 6.50 per unit.
Design an object-oriented PHP program that takes the consumed units as input
and calculates the corresponding electricity bill for a household. Implement this
program using appropriate class(es), method(s), and if-else conditions for rate
determination.
Sol)
This is Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<?php
$units = $_POST['units'];
if(!empty($units)){
$result = calculate($units);
}
function calculate($units){
if($units<=50){
$total = 3.50*50;
}else if($units>50 and $units<=100){
$prim_amount = 3.50*50;
$sec_amount = ($units-50)*4.00;
$total = $prim_amount+$sec_amount;
}else if($units>100 && $units<=200){
$prim_amount = (3.50*50) + (4.00*100);
$sec_amount = ($units-150)*5.20;
$total = $prim_amount+$sec_amount;
}else {
$prim_amount = (50*3.50)+(4.00*100)+(5.20*100);
$sec_amount = ($units-250)*6.50;
$total = $prim_amount+$sec_amount;
}
return $total;
}
?>
<body>
<form action="" method="post">
<input type="number" name="units" id="units" placeholder="Enter numbr
of units">
<input type="submit">
<?php
echo "<br>"."The Electricity bill of {$units} is: ".$result."/-";
?>
</form>
</body>
</html>
Explanation:
HTML Structure:
-> A basic HTML page with a form where users can enter the number of
electricity units.
-> The form uses the POST method to send input data to the same page for
processing.
PHP Code:
-> The PHP script retrieves the units from the form using $_POST['units'].
-> If units are entered the calculate() function is called
3.50 per unit for the first 50 units.
4.00 per unit for the next 50 units (up to 100).
5.20 per unit for units between 100 and 200.
6.50 per unit for units above 250.
Thank you.