PRACTICE PROGRAM:
1. Write a PHP script to calculate the area and volume of a cylinder using a function.
ANS:
HTML 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>
<body>
<h1>calculate area and volume of cylinder</h1>
<form action="<?php echo$_SERVER['PHP_SELF']?>" method="POST">
<label for="radius"><b>radius</b></label>
<input type="number" name="radius">
<br>
<label for="height"><b>height</b></label>
<input type="number" name="height">
<br>
<label for="volume"><b>volume is:</b></label>
<input type="text" name="volume" value="<?php echo$volume ?>" readonly>
<br>
<label for="area"><b>area is:</b></label>
<input type="text" name="area" value="<?php echo$area ?>" readonly>
<br>
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset" id="reset" value="cancel">
</form>
</body>
</html>
PHP CODE:
<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
{
if(isset($_POST["submit"]))
{
$r=$_POST["radius"];
$h=$_POST["height"];
define('pi','3.14');
function area($r,$h)
{
$a=2*(pi*$r*$h)+2*(pi*$r*$r);
return $a;
}
function volume($r,$h)
{
$v=pi*$r*$r*$h;
return $v;
}
$area=area($r,$h);
$volume=volume($r,$h);
}
}
?>
--------------------------------------------------------------------------------------------------
2. Write a PHP Script to display the sum and average of array elements(Using predefined
functions)
ANS:
PHP CODE:
<?php
error_reporting(0);
$a=array(1,2,3,4,5,6,7,8,9,10);
$sum=array_sum($a);
echo("Sum of array element is:".$sum."<br>");
$avg=$sum/count($a);
echo("Average of array element is:".$avg."<br>");
?>
--------------------------------------------------------------------------------------------------
3.. Write a PHP script to calculate the factorial of a number using a function.
ANS:
HTML 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>
<body>
<h1>
calculate factorial using function!
</h1>
<form action="<?php echo$_SERVER['PHP_SELF']?>" method="POST">
<label for="num"><b>Enter number:</b></label>
<input type="number" name="num" id="num">
<br>
<label for="fact"><b>Factorial is:</b></label>
<input type="text" name="fact" id="fact" value="<?php echo$result ?>" readonly>
<br>
<input type="submit" name="submit" id="submit">
<input type="reset" name="reset" id="reset">
</form>
</body>
</html>
PHP CODE:
<?php
error_reporting(0);
if(isset($_POST["submit"]))
{
$num=$_POST["num"];
function factorial($num)
{
$f=1;
for($i=1;$i<=$num;$i++)
{
$f=$f*$i;
}
return $f;
}
$result=factorial($num);
}
?>
------------------------------------------------------------------------------------------------
SET A:
1. Write a PHP script to calculate xy
using a function.
ANS:
HTML 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>
<body>
<h1>
Calculate x<sup>y</sup> using function!
</h1>
<form action="<?php echo$_SERVER['PHP_SELF'] ?>" method="POST">
<label for="x"><b>Enter coefficient:</b></label>
<input type="number" name="x" id="x"><br>
<br>
<label for="y"><b>Enter power :</b></label>
<input type="number" name="y" id="y"><br>
<br>
<label for="ans"><b>x<sup>y</sup>is:</b></label>
<input type="text" name="ans" id="ans" value="<?php echo$result ?>" readonly>
<br>
<input type="submit" name="submit" id="submit" value="submit">
</form>
</body>
</html>
PHP CODE:
<?php
if(isset($_POST["submit"]))
{
$x=$_POST["x"];
$y=$_POST["y"];
function power($x,$y)
{
$ans=1;
for ($i=1; $i <= $y; $i++) {
$ans=$ans*$x;
}
return $ans;
}
$result=power($x,$y);
}
?>
---------------------------------------------------------------------------------------
2. Write a PHP script to define a function EvenOdd, which will display even and odd numbers
between 1 to 50.
ANS:
PHP CODE:
<?php
function evenOdd()
{
$e=array();
$o=array();
for ($i=1; $i < 50; $i++) {
if($i%2==0)
{
$e[$i]=$i;
}else{
$o[$i]=$i;
}
}
echo"Total even numbers are:"."<br>";
foreach($e as $j)
{
echo$j.",";
}
echo"<br>";
echo"Total odd numbers are:"."<br>";
foreach($o as $j)
{
echo$j.",";
}
}
evenOdd();
?>
-----------------------------------------------------------------------------------------
3. Write a PHP script to define a function Maximum, which will accept 3 numbers as
parameters and returns a maximum of 3 numbers.
ANS:
HTML 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>
<body>
<h1>Maximum of Three numbers!</h1>
<form action="<?php echo$_SERVER['PHP_SELF'];?>" method="POST">
<label for="num1"><b>Enter first number:</b></label>
<input type="number" name="num1" id="num1">
<br>
<label for="num2"><b>Enter second number:</b></label>
<input type="number" name="num2" id="num2">
<br>
<label for="num3"><b>Enter third number:</b></label>
<input type="number" name="num3" id="num3">
<br>
<input type="submit" name="submit" id="submit" value="submit">
<input type="reset" name="reset" id="reset" value="reset">
</form>
</body>
</html>
PHP CODE:
<?php
error_reporting(0);
if(isset($_POST["submit"]))
{
$a=$_POST["num1"];
$b=$_POST["num2"];
$c=$_POST["num3"];
function maximum($p,$q,$r)
{
if($p>$q&&$p>$r)
{
return $p;
}else if($q>$p&&$q>$r){
return $q;
}else{
return $r;
}
}
$ans=maximum($a,$b,$c);
echo"<script>alert('Maximum number is $ans');</script>";
}
?>
------------------------------------------------------------------------------------
4. Write a PHP script to swap two numbers using a function (Use Call by value and Call by
reference)
ANS:
HTML 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>
<body>
<h1>Swap two numbers using function!</h1>
<form action="<?php echo$_SERVER['PHP_SELF']?>" method="POST">
<label for="num1"><b>Enter first number:</b></label>
<input type="number" name="num1" id="num1"><br>
<br>
<label for="num2"><b>Enter second number:</b></label>
<input type="number" name="num2" id="num2"><br>
<br>
<input type="submit" name="submit" id="submit" value="submit">
<input type="reset" name="reset" id="reset" value="cancel">
</form>
</body>
</html>
PHP CODE:
<?php
error_reporting(0);
if (isset($_POST["submit"])) {
$a=$_POST["num1"];
$b=$_POST["num2"];
function swapByValue($a,$b)
{
echo" before The swap value is: "."a=".$a." and "."b=".$b."<br>";
$a=$a+$b; //$a=10+20;
$b=$a-$b; //$a=30;$b=30+20;$b=50;$a=30-50;$a=-20
$a=$a-$b;
echo"The swap value is(using call by value): "." a=".$a." and "." b=".$b."<br>";
}
function swapByReference(&$a,&$b)
{
echo" before The swap value is: "." a=".$a." and "."b=".$b."<br>";
$a=$a+$b; //$a=10+20;
$b=$a-$b; //$a=30;$b=30+20;$b=50;$a=30-50;$a=-20
$a=$a-$b;
echo"The swap value is(using call by reference): "." a=".$a." and "." b=".$b."<br>";
}
swapByValue($a,$b);
swapByReference($a,$b);
}
?>
-------------------------------------------------------------------------------------------------------------------
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.
ANS:
PHP CODE:
<?php
class fruit{
public $name;
public $color;
public $price;
function display()
{
echo $this->name."<br>";
echo $this->color."<br>";
echo $this->price."<br>";
}
}
$f1=new fruit();
$f1->name="apple";
$f1->color="red";
$f1->price=100;
$f1->display();
$f2=new fruit();
$f2->name="banana";
$f2->color="yellow";
$f2->price=30;
$f2->display();
?>
----------------------------------------------------------------------------------------------------------
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.
ANS:
PHP CODE:
<?php
class student{
public $roll_number;
public $student_name;
public $percentage;
function studentInformation()
{
echo $this->roll_number."<br>";
echo $this->student_name."<br>";
echo $this->percentage."<br>";
}
}
$s1=new student();
$s1->student_name="rakesh";
$s1->roll_number=235252;
$s1->percentage=80;
$s1->studentInformation();
?>
-----------------------------------------------------------------------------------------------------------
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.
ANS:
PHP CODE:
<?php
class book{
public $book_id;
public $book_name;
public $publication;
public $author;
public $book_price;
function book_details()
{
echo "The id of book :"."<br>".$this->book_id."<br>";
echo "The name of book :"."<br>".$this->book_name."<br>";
echo "The publication of book :"."<br>".$this->publication."<br>";
echo "The author of book :"."<br>".$this->author."<br>";
echo "The price of book :"."<br>".$this->book_price."<br>";
}
}
$b1=new book();
$b1->book_id=101;
$b1->book_name="mrutunjay";
$b1->book_price=300;
$b1->publication="nirali";
$b1->author="vijay sawant";
$b1->book_details();
?>
---------------------------------------------------------------------------------------------------------
SET C:
1. Write a PHP script to define a function “DisplayDay”, which will display the day of the
current date. ANS:
PHP CODE:
<?php
function displayDay()
{
echo date("D")."day";
}
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 parameters)
ANS:
PHP CODE:
<?php
function arithmetic($a=10,$b=20)
{
$result=$a+$b;
echo "addition is : ".$result."<br>";
$result=$a-$b;
echo "subtraction is : ".$result."<br>";
$result=$a*$b;
echo "multiplication is : ".$result."<br>";
$result=$a/$b;
echo "division is : ".$result."<br>";
$result=$a%$b;
echo "mod is : ".$result."<br>";
}
arithmetic();
?>
-------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment
Enter comment here!