Exercise Solved
Exercise Solved
Exercise Solved
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;
?>
<body>
<div id="page-wrap">
<h1>Php - Calculate Electricity Bill</h1>
<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
Solution:
<?php
$count = 5;
while($count <= 15){
echo "$count";
echo "<br>" ;
$count++;
}
?>
5. Write a program to print “Hello World” using echo only?
Conditions:
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:
Solution
<?php
$text = "PHP World";
echo "Welcome to the $text";
?>
8. Write a program to print 2 php variables using single echo statement.
Conditions:
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:
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;
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>