Download File => Click
Practice Programs:
1. Write a PHP script to calculate the area and volume of a cylinder using a function.
define("PI", 3.14);
$radius = 3;
$height = 5;
function areaOfCylinder($radius, $height) {
$area = (2 * PI * $radius *
$height) + (2 * PI * $radius * $radius);
return $area;
}
function volumeOfCylinder($radius, $height) {
$volume = PI * $radius * $radius
* $height;
return $volume;
}
$cylinder_area = areaOfCylinder(4, 5);
echo "The Area of cylinder is = $cylinder_area <br>";
$cylinder_volume = volumeOfCylinder(3, 6);
echo "The volume of cylinder is = $cylinder_volume";
?>
=================================================================
2. Write a PHP Script to display the sum and average of array
elements(Using predefined functions)
<?php
$arr = [1,2,3,4,5,88,44,1,33,9];
$result = array_sum($arr);
echo "The sum of array is = $result <br>";
$avg = $result / count($arr);
echo "The average is = $avg <br>";
?>
3. Write a PHP script to calculate the factorial of a number using a
function.
<?php
$num = 5;
$cpy = $num;
$fact = 1;
while($num > 0) {
$fact = $fact * $num;
$num = $num - 1;
}
echo "Number is $cpy and there factorial is = $fact";
?>
SET A:
1. Write a PHP script to calculate x^y using a function.
<?php
$x = 5;
$y = 5;
$result = power($x, $y);
echo "The $x^$y is =
$result";
function power($x, $y) {
return $x ** $y;
}
?>
==========================================================
2. Write a PHP script to define a function EvenOdd, which will display even
and odd numbers between 1 to 50.
<?php
function evenDisplay() {
echo "Total even numbers
between 1 to 50 <br>";
for ($i=1; $i <= 50; $i++)
{
if($i % 2 == 0) {
echo "$i
<br>";
}
}
}
function oddDisplay() {
echo "Total odd numbers
between 1 to 50 <br>";
for ($i=1; $i <= 50; $i++)
{
if($i % 2 != 0) {
echo "$i
<br>";
}
}
}
evenDisplay();
oddDisplay();
?>
===========================================================
3. Write a PHP script to define a function Maximum, which will accept 3
numbers as parameters and returns a maximum of 3 numbers.
<?php
$result = maximumThreeNo(5, 10,
15);
echo "The Maximum number is
= $result <br>";
function maximumThreeNo($a, $b,
$c) {
if($a > $b && $a
> $c) {
return $a;
} elseif($b > $a
&& $b > $c) {
return $b;
} else {
return $c;
}
}
?>
=============================================================
4. Write a PHP script to swap two numbers using a function (Use Call by
value and Call by reference)
<?php
$a = 10;
$b = 20;
echo "Before A and B is $a
and $b <br>";
swapByref($a, $b);
echo "After A and B is $a
and $b <br>";
swapByValue(20, 40);
// Call by reference
function swapByref(&$a,
&$b) {
$temp = $b;
$b = $a;
$a = $temp;
}
function swapByValue($a, $b) {
echo "Inside
swapByValue function: <br>";
$temp = $a;
$a = $b;
$b = $temp;
echo "Num1 : $a
<br>";
echo "Num2 : $b
<br>";
}
?>
============================================================
SET B:
1. Write a PHP Script to create a class Fruit that contains data members as
Name, Color and Price. Write a member function to accept and display details of
Fruit.
HTML File :
<!DOCTYPE html>
<html lang="en">
<head>
<meta
charset="UTF-8" />
<meta
name="viewport" content="width=device-width,
initial-scale=1.0" />
<title>Document</title>
<style>
label {
display: block;
}
</style>
</head>
<body>
<h1>Enter fruit
Details</h1>
<form
action="01_fruits_class.php" method="post">
<label
for="">Enter fruit Name : </label>
<input
type="text" name="fName" id="" />
<label
for="">Enter fruit Color : </label>
<input
type="text" name="fColor" id="" />
<label
for="">Enter fruit Price : </label>
<input
type="text" name="fPrice" id="" />
<input
type="submit" value="Show Details" />
</form>
</body>
</html>
--------------------------------------
PHP File :
<?php
class Fruit {
public $name, $color, $price;
public function
acceptDetails() {
$this->name =
$_POST["fName"];
$this->color =
$_POST["fColor"];
$this->price =
$_POST["fPrice"];
}
public function showDetails()
{
echo "<h2>
Fruits Details </h2>";
echo "Name = $this->name
<br>";
echo "Color =
$this->color <br>";
echo "Price =
$this->price <br>";
}
}
$f1 = new Fruit();
$f1->acceptDetails();
$f1->showDetails();
?>
=================================================================
2. Write a PHP Script to create a class Student that contains data members
as Roll_Number, Stud_Name, and Percentage. Write member functions to accept
Student information.
HTML File :
<!DOCTYPE html>
<html lang="en">
<head>
<meta
charset="UTF-8" />
<meta
name="viewport" content="width=device-width,
initial-scale=1.0" />
<title>Document</title>
<style>
label {
display: block;
}
</style>
</head>
<body>
<h2>Enter student
Details</h2>
<form
action="02_student_class.php" method="post">
<label
for="">Enter Student Roll No : </label>
<input
type="text" name="sRoll" id="" />
<label for="">Enter
Student Name : </label>
<input
type="text" name="sName" id="" />
<label
for="">Enter Student Percentage : </label>
<input
type="text" name="sPer" id="" />
<input
type="submit" value="Show Details" />
</form>
</body>
</html>
----------------------------------
PHP File :
<?php
class Students {
public $Roll_Number, $Stud_Name,
$Percentage;
public function putDetail($r, $n,
$p) {
$this->Roll_Number = $r;
$this->Stud_Name = $n;
$this->Percentage = $p;
}
public function show() {
echo "<h2> Student
Details </h2>";
echo "Name =
$this->Roll_Number <br>";
echo "Name =
$this->Stud_Name <br>";
echo "Percentage =
$this->Percentage% <br>";
}
}
$s1 = new Students();
$s1->putDetail($_POST['sRoll'],$_POST['sName'],$_POST['sPer'],);
$s1->show();
?>
3. Write a PHP Script to create a class Book (Book_id, Book_name, Publication,
Author, Book_price). Write a member function to accept and display Book
details.
<?php
class Book {
public $Book_id;
public $Book_name;
public $Publication;
public $Author;
public $Book_price;
public function __construct($id,
$name, $pub, $author, $price) {
$this->Book_id = $id;
$this->Book_name = $name;
$this->Publication = $pub;
$this->Author = $author;
$this->Book_price =
$price;
}
public function bookDetails() {
echo "<h2> Book
Details </h2>";
echo "Id =
$this->Book_id <br>";
echo "Name =
$this->Book_name <br>";
echo "Publication =
$this->Publication <br>";
echo "Author =
$this->Author <br>";
echo "Price =
$this->Book_price <br>";
}
}
$b1 = new Book(1, "PHP",
"Bal Bharati", "K.K.
Ram", 150);
$b1->bookDetails();
?>
==================================================================
SET C:
1. Write a PHP script to define a function “DisplayDay”, which will display
the day of the current date.
<?php
function DisplayDay() {
$currentDate =
date("Y-m-d");
$timestamp =
strtotime($currentDate);
$dayOfWeek = date("l");
echo "Today is " .
$dayOfWeek;
}
DisplayDay();
?>
2. Write a PHP script to perform arithmetic operations on two numbers.
Write a PHP function to display the result. (Use the concept of function and
default arameters)
<?php
$num1 = 10;
arithmetic($num1);
function arithmetic($a, $b = 2) {
echo "<h2> Arithmetic Operation
</h2>";
echo "<br> The
Addition is ". ($a + $b);
echo "<br> The
Subtraction is ". ($a - $b);
echo "<br> The
Multiplication is ". ($a * $b);
echo "<br> The
Division is ". ($a / $b);
}
?>
================================================================
0 Comments