Exercise Solved

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Php exercises

2. write a PHP program to calculate electricity bill using if-else conditions.


Conditions:

 For first 50 units – Rs. 3.50/unit


 For next 100 units – Rs. 4.00/unit
 For next 100 units – Rs. 5.20/unit
 For units above 250 – Rs. 6.50/unit
 You can use conditional statements.

Solution

<!DOCTYPE html>

<head>
<title>PHP - Calculate Electricity Bill</title>
</head>

<?php
$result_str = $result = '';
if (isset($_POST['unit-submit'])) {
$units = $_POST['units'];
if (!empty($units)) {
$result = calculate_bill($units);
$result_str = 'Total amount of ' . $units . ' - ' . $result;
}
}
/**
* To calculate electricity bill as per unit cost
*/
function calculate_bill($units) {
$unit_cost_first = 3.50;
$unit_cost_second = 4.00;
$unit_cost_third = 5.20;
$unit_cost_fourth = 6.50;

if($units <= 50) {


$bill = $units * $unit_cost_first;
}
else if($units > 50 && $units <= 100) {
$temp = 50 * $unit_cost_first;
$remaining_units = $units - 50;
$bill = $temp + ($remaining_units * $unit_cost_second);
}
else if($units > 100 && $units <= 200) {
$temp = (50 * 3.5) + (100 * $unit_cost_second);
$remaining_units = $units - 150;
$bill = $temp + ($remaining_units * $unit_cost_third);
}
else {
$temp = (50 * 3.5) + (100 * $unit_cost_second) + (100 * $unit_cost_third);
$remaining_units = $units - 250;
$bill = $temp + ($remaining_units * $unit_cost_fourth);
}
return number_format((float)$bill, 2, '.', '');
}

?>

<body>
<div id="page-wrap">
<h1>Php - Calculate Electricity Bill</h1>

<form action="" method="post" id="quiz-form">


<input type="number" name="units" id="units" placeholder="Please enter no. of Units" />
<input type="submit" name="unit-submit" id="unit-submit" value="Submit" />
</form>

<div>
<?php echo '<br />' . $result_str; ?>
</div>
</div>
</body>
</html>
3. write a simple calculator program in PHP using switch case.
Operations:

 Addition
 Subtraction
 Multiplication
 Division

Solution

<!DOCTYPE html>
<head>
<title>Simple Calculator Program in PHP - Tutorials Class</title>
</head>

<?php
$first_num = $_POST['first_num'];
$second_num = $_POST['second_num'];
$operator = $_POST['operator'];
$result = '';
if (is_numeric($first_num) && is_numeric($second_num)) {
switch ($operator) {
case "Add":
$result = $first_num + $second_num;
break;
case "Subtract":
$result = $first_num - $second_num;
break;
case "Multiply":
$result = $first_num * $second_num;
break;
case "Divide":
$result = $first_num / $second_num;
}
}

?>

<body>
<div id="page-wrap">
<h1>PHP - Simple Calculator Program</h1>
<form action="" method="post" id="quiz-form">
<p>
<input type="number" name="first_num" id="first_num" required="required" value="<?php echo $first_num;
?>" /> <b>First Number</b>
</p>
<p>
<input type="number" name="second_num" id="second_num" required="required" value="<?php echo
$second_num; ?>" /> <b>Second Number</b>
</p>
<p>
<input readonly="readonly" name="result" value="<?php echo $result; ?>"> <b>Result</b>
</p>
<input type="submit" name="operator" value="Add" />
<input type="submit" name="operator" value="Subtract" />
<input type="submit" name="operator" value="Multiply" />
<input type="submit" name="operator" value="Divide" />
</form>
</div>
</body>
</html>
4. Write a Program to display count, from 5 to 15 using PHP loop as given
below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Rules & Hint

 You can use “for” or “while” loop


 You can use variable to initialize count
 You can use html tag for line break

Solution:

<?php
$count = 5;
while($count <= 15){
echo "$count";
echo "<br>" ;

$count++;
}
?>
5. Write a program to print “Hello World” using echo only?
Conditions:

 You can not use any variable.

Solution

<?php
echo "Hello World";
?>
6. Write a program to print “Hello PHP” using php variable?
Conditions:

 You can not use text directly in echo but can use variable.

Solution

<?php
$message = "Hello PHP";
echo "$message";
?>
7. Write a program to print “Welcome to the PHP World” using some part of
the text in variable & some part directly in echo.
Conditions:

 You have to use a variable that contains string “PHP World”.

Solution
<?php
$text = "PHP World";
echo "Welcome to the $text";
?>
8. Write a program to print 2 php variables using single echo statement.
Conditions:

 First variable have text “Good Morning.”


 Second variable have text “Have a nice day!”
 Your output should be “Good morning. Have a nice day!”
 You are allowed to use only one echo statement in this program.

Solution

<?php
$message_1 = "Good Morning.";
$message_2 = "Have a nice day!";
echo "$message_1 $message_2";

?>

9. Write a program to check student grade based on the marks using if-else
statement.
Conditions:

 If marks are 60% or more, grade will be First Division.


 If marks between 45% to 59%, grade will be Second Division.
 If marks between 33% to 44%, grade will be Third Division.
 If marks are less than 33%, student will be Fail.
 <?php
 $marks = 40;

 if ($marks>=60)
 {
 $grade = "First Division";
 }
 else if($marks>=45)
 {
 $grade = "Second Division";
 }
 else if($marks>=33)
 {
 $grade = "Third Division";
 }
 else
 {
 $grade = "Fail";
 }

 echo "Student grade: $grade";
 ?>
10. Write a program to show day of the week (for example: Monday) based
on numbers using switch/case statements.
Conditions:

 You can pass 1 to 7 number in switch


 Day 1 will be considered as Monday
 If number is not between 1 to 7, show invalid number in default

Solution

<?php
$day = "5";

switch ($day) {
case "1":
echo "It is Monday!";
break;
case "2":
echo "It is today!";
break;
case "3":
echo "It is Wednessday!";
break;
case "4":
echo "It is Thursday!";
break;
case "5":
echo "It is Friday!";
break;
case "6":
echo "It is Saturday!";
break;
case "7":
echo "It is Sunday!";
break;
default:
echo "Invalid number!";
}
?>

11. Write a program to calculate factorial of a number using for loop in php.

<?php
$num = 3;
$factorial = 1;

for ($x=$num; $x>=1; $x--)


{
$factorial = $factorial * $x;
}

echo "The factorial of $num is $factorial";


?>
12. Write a PHP program to find factorial of a number using
recursive function.
What is Recursive Function?

 A recursive function is a function that calls itself.


 <?php
 function factorial($number) {

 if ($number < 2) {
 return 1;
 } else {
 return ($number * factorial($number-1));
 }
 }

 echo factorial(4);
 ?>
13. Write a PHP program using nested for loop that creates a chess board.
Conditions:

 You can use html table having width=”400px” and take “30px” as cell
height and width for check boxes.
 <table width="400px" cellspacing="0px" cellpadding="0px" border="1px">
 <?php
 for($row=1;$row<=8;$row++)
 {
 echo "<tr>";
 for($column=1;$column<=8;$column++)
 {
 $total=$row+$column;
 if($total%2==0)
 {
 echo "<td height=35px width=30px bgcolor=#FFFFFF></td>";
 }
 else
 {
 echo "<td height=35px width=30px bgcolor=#000000></td>";
 }
 }
 echo "</tr>";
 }
 ?>
 </table>

You might also like