You are on page 1of 46

Pacific School Of Engineering 219760307017

Practical – 1
Aim: Write a script to display Fibonacci numbers up to given term.
Code:
<html>

<head>

<title>Fibonacci</title>

</head>

<h3>Fibonacci Series: </h3>

<body>

<?php

$a=0;

$b=1;

for($i=0; $i< 10; $i++){

echo $a;

echo " ";

$c=$b+$a;

$a=$b;

$b=$c;

?>

</body>

</html>

Output:

IWD Page 1
Pacific School Of Engineering 219760307017

Practical – 2
Aim: Write a script to display the merge to string.
Code:
<html>

<head>

<title>php Web page</title>

</head>

<body>

<?php

$a= "Hello ";

$b= "World!";

$c= $a.$b;

echo $c;

?>

</body>

</html>

Output:

IWD Page 2
Pacific School Of Engineering 219760307017

Practical – 3
Aim: Write a script to display a multiplication table for the given number.
Code:
<html>

<head><title>php Web page</title></head>

<body>

<form method="POST">

<div><label for = "num">Enter a Table: </label>

<input type="number" name= "num" id="num"></input>

<button type="submit" name = "submit">Submit</button></div>

</form>

<?php

if(isset($_POST['submit'])){

$a = $_POST['num'];

for ($i=1; $i<=10; $i++)

$result = $a*$i;

echo "$a X $i = $result <br>";}}

?>

</body>

</html>

Output:

IWD Page 3
Pacific School Of Engineering 219760307017

Practical – 4
Aim: Write a script to implement Simple Calculator for mathematical
operations.
Code:
<?php

if(isset($_GET['operation']))

{ $x = $_GET['num1'];

$y = $_GET['num2'];

$op = $_GET['operation'];

if(is_numeric($x) && is_numeric($y))

{ switch($op)

{ case 'add':

$result = $x + $y;

break;

case 'sub':

$result = $x - $y;

break;

case 'product':

$result = $x * $y;

break;

case 'division':

$result = $x / $y;

break;

}}}

?>

<html>

<head><title>Calculater</title></head>

<body>

IWD Page 4
Pacific School Of Engineering 219760307017

<form action="<?= $_SERVER['PHP_SELF'] ?>" method= "get">

<div> <label for="num1">Number 1:</label>

<input type="number" name="num1" id="num1" value="<?= $x?>">

</div>

<div> <label for="num2">Number 2: </label>

<input type="number" name="num2" id="num2" value="<?= $y?>">

</div>

<div>

<label for="result"> Result: </label>

<input type="number" id="result" value="<?= $result?>">

</div>

<div>

<input type="submit" name="operation" value="add">

<input type="submit" name="operation" value="sub">

<input type="submit" name="operation" value="product">

<input type="submit" name="operation" value="division">

</div>

</form>

</body>

</html>

Output:

IWD Page 5
Pacific School Of Engineering 219760307017

Practical – 5
Aim: Write a script to take the basic salary of an employee as input and
calculate the net payment to any employee.
Code:
<html>

<head>

<title>Salary</title>

</head>

<body>

<form method="POST">

<div>

<label for="salary">Enter your Basic Salary:</label>

<input type="number" name="salary" placeholder="Your Salary" >

</div>

<div>

<button type="submit" name="submit" value="submit">Submit</button>

</div>

</form>

<?php

if(isset($_POST['submit']))

$basic_salary=$_POST['salary'];

$da= 0.5 * $basic_salary;

$hra= 0.1 * $basic_salary;

$medical= 0.04 * $basic_salary;

$gross_salary= $basic_salary+$da+$hra+$medical;

$ins=0.07 * $gross_salary;

IWD Page 6
Pacific School Of Engineering 219760307017

$pf=0.05 * $gross_salary;

$deduction=$ins+$pf;

$net_salary= $gross_salary - $deduction;

echo "Basic Salary : ".$basic_salary." /- <br>";

echo "Dearness Allowance : ".$da." /-<br>";

echo "House Rent Allowance : ".$hra." /- <br>";

echo "Gross Salary : ".$gross_salary." /- <br>";

echo "Deduction : ".$deduction." /- <br>";

echo "Net Salary : ".$net_salary." /- <br>";

return 0;

?>

</body>

</html>

Output:

IWD Page 7
Pacific School Of Engineering 219760307017

Practical – 6
Aim: Write a script that reads the name of the car and displays the name of the company the car
belongs to.

Code:
<html>

<head>

<title>Cars</title>

</head>

<body>

<form method="POST">

<div>

<label for="company">Enter company name:</label>

<select name="company">

<option value="BMW">BMW</option>

<option value="Tata">Tata</option>

<option value="Mahindra">Mahindra</option>

<option value="Hyundai">Hyundai</option>

<option value="Suzuki">Suzuki</option>

<option value="Hyundai">Hyundai</option>

</select>

</div>

<div>

<button type="submit" name="submit" value="submit">Submit</button>

</div>

</form>

<?php

if(isset($_POST['submit']))

IWD Page 8
Pacific School Of Engineering 219760307017

$com=$_POST['company'];

switch ($com)

case "Tata":

echo "Comapny Name : Tata <br>";

echo "it's car models are : Safari, Nexon, Tigor, Tiago <br>";

break;

case "Mahindra":

echo "Comapny Name : Mahindra <br>";

echo "it's car models are : XUV700, XUV300, Bolero <br>";

break;

case "Hyundai":

echo "Comapny Name : Hyundai <br>";

echo "it's car models are : i20, Verna, Venue, Creta <br>";

break;

case "Suzuki":

echo "Comapny Name : Suzuki <br>";

echo "it's car models are : Swift, Alto, Baleno, Brezza <br>";

break;

case "BMW":

echo "Comapny Name : BMW <br>";

echo "it's car models are : i8,x5, x7, x8 <br>";

break;

default:

echo "your company name is not in our database";

break;

return 0;

IWD Page 9
Pacific School Of Engineering 219760307017

?>

</body>

</html>

Output:

IWD Page 10
Pacific School Of Engineering 219760307017

Practical – 7
Aim: Write a script to read the marks of 4 subjects and display the result.

Code:
<html>

<head><title>Result</title></head>

<body>

<form action="<?= $_SERVER['PHP_SELF'] ?>" method= "get">

<div>

<label for="sub1">Subject 1:</label>

<input type="number" name="sub1" id="sub1" value="<?= $a?>">

</div>

<div>

<label for="sub2">Subject 2:</label>

<input type="number" name="sub2" id="sub2" value="<?= $b?>">

</div>

<div>

<label for="sub3">Subject 3:</label>

<input type="number" name="sub3" id="sub3" value="<?= $c?>">

</div>

<div>

<label for="sub4">Subject 4:</label>

<input type="number" name="sub4" id="sub4" value="<?= $d?>">

</div>

<div>

<button type="submit" name="submit" value="submit">Submit</button>

</div>

<?php

if(isset($_GET['submit'])){

IWD Page 11
Pacific School Of Engineering 219760307017

$a=$_GET['sub1'];

$b=$_GET['sub2'];

$c=$_GET['sub3'];

$d=$_GET['sub4'];

$sum=$a+$b+$c+$d;

$per=($sum/400)*100;

if ($a<=35 || $b<=35 || $c<=35 || $d<=35){

echo "You are Fail...<br>";

echo "Your Total Marks: $sum<br>";

echo "Your Pertange: $per<br>";

echo "Your GTU Grade: FF";

return 0;

else

echo "You are Pass...<br>";

echo "Your Total Marks: $sum<br>";

echo "Your Pertange: $per<br>";

if($per>=85 && $per<=100)

{echo "Your GTU Grade: AA";}

elseif($per>=75 && $per<=84)

{echo "Your GTU Grade: AB";}

elseif($per>=65 && $per<=74)

{echo "Your GTU Grade: BB";}

elseif($per>=55 && $per<=64)

{echo "Your GTU Grade: BC";}

elseif($per>=45 && $per<=54)

IWD Page 12
Pacific School Of Engineering 219760307017

{echo "Your GTU Grade: CC";}

elseif($per>=40 && $per<=44)

{echo "Your GTU Grade: CD";}

elseif($per>=35 && $per<=39)

{echo "Your GTU Grade: DD";}

elseif($per<=35)

{echo "Your GTU Grade: FF";}

else{echo"Invalid value...";}

?>

</form>

</body>

</html>

Output:

IWD Page 13
Pacific School Of Engineering 219760307017

Practical – 8
Aim: Write a script to calculate the length of a string and count the number of Words in the given
string Without using string functions.

Code:
<html>

<head>

<title>Php Practical</title>

</head>

<body>

<?php

$str ="this is php";

$i = 0;

while (@$str[$i++] != NULL);

$i--;

echo "String : $str<br>";

echo "length of String: $i<br>";

$word = str_word_count($str);

echo "word inside given String: $word";

?>

</body>

</html>

Output:

IWD Page 14
Pacific School Of Engineering 219760307017

Practical – 9
Aim: Write a script to calculate the length of a string and count the number of Words in the given
string With using string functions.

Code:
<html>

<head><title>Php Practical</title></head>

<body>

<?php

$str ="this is php";

$len = strlen($str);

echo "String : $str<br>";

echo "length of String: $len<br>";

$word = str_word_count($str);

echo "word inside given String: $word";

?>

</body>

</html>

Output:

IWD Page 15
Pacific School Of Engineering 219760307017

Practical – 10
Aim: Write a script to sort a given indexed array.
Code:
<html>

<head>

<title>Php Practical</title>

</head>

<body>

<?php

$arr = array(5,4,3,1,2);

sort($arr);

echo "Elements of array: <br>";

foreach ($arr as $a)

echo "$a <br>";

?>

</body>

</html>

Output:

IWD Page 16
Pacific School Of Engineering 219760307017

Practical – 11
Aim: Write a script to perform 3 x 3 matrix Multiplication.

Code:
<html>

<head><title>Php Practical</title></head>

<body>

<?php

$a = array (array (1,2,3),array (1,2,3),array (1,2,3));

$b = array (array (1,2,3),array (1,2,3),array (1,2,3));

$result = array ();

for ($i=0; $i<3; $i++)

{for ($j=0;$j<3; $j++)

{$result[$i][$j] = 0;

for ($k=0; $k<3; $k++){

$result[$i][$j] += $a[$i][$k]*$b[$k][$j];

}} }

echo "Multiplication of 3X3 matrix: <br>";

for ($i=0; $i<3; $i++)

{for ($j=0;$j<3; $j++)

{echo $result[$i][$j]." ";}

echo "<br>"; }

?>

</body></html>

Output:

IWD Page 17
Pacific School Of Engineering 219760307017

Practical – 12
Aim: Write a script to encode a given message into equivalent Morse code.
Code:
<html>

<head><title>Php Practical</title></head>

<form method="POST">

<div>

<label for="string">Enter a string: </label>

<input type="text" name="string">

<button type="submit" name="submit">Submit</button>

</div>

</form>

<body>

<?php

if (isset($_POST["submit"])){

$string= $_POST["string"];

echo "String is : $string<br>";

$str = strtolower($string);

$morse_code = array("a" => ".-","b" => "-...","c" => "-.-.","d" => "-..","e" => ".","f" => "..-.","g" => "--
.","h" => "....", "i" => "..","j" => ".---","k" => "-.-","l" => ".-..","m" => "--","n" => "-.","o" => "---","p" =>
".--.","q" => "--.-","r" => ".-.","s" => "...","t" => "-","u" => "..-","v" => "...-","w" => ".--","x" => "-..-","y"
=> "-.--","z" => "--..", "0" => "-----","1" => ".----","2" => "..---","3" => "...--","4" => "....-","5" => " ..... ","6"
=> "-....","7" => "--...","8" => "---..", "9" => "----.","." => ".-.-.-","," => "--..--","?" => "..--..","/" => "-..-
."," " =>" ");

for ($i=0; $i<strlen($str); $i++)

foreach ($morse_code as $char=>$code)

{if ($char === $str[$i])

{echo "<h1>$code </h1>";}

IWD Page 18
Pacific School Of Engineering 219760307017

?>

</body>

</html>

Output:

IWD Page 19
Pacific School Of Engineering 219760307017

Practical – 13
Aim: Consider a currency system in Which there are notes of 7 denominations, namely Rs. 1, Rs. 2,
Rs. 5, Rs. 10, Rs. 20, Rs. 50 and Rs. 100. Write a function that computes the smallest number of notes
that Will combine for a given amount of money.

Code:
<html>

<head><title>Php Practical</title</head>

<body>

<?php

function countCarrency($amount){

$notes = array (2000,500,100,50,20,10,5,2,1);

$noteCounter= array (0,0,0,0,0,0,0,0,0,0);

for ($i=0;$i<9; $i++)

{if ($amount >= $notes[$i])

{$noteCounter[$i] = intval($amount / $notes[$i]);

$amount = $amount % $notes[$i];}}

echo "Currency Count -> <br>";

for ($i=0;$i<9; $i++)

{if ($noteCounter[$i]!=0)

{echo "$notes[$i] : $noteCounter[$i] <br>";}}}

$amount = 2868;

echo "Amount is $amount <br><br>";

countCarrency ($amount);

?>

</body>

</html>

IWD Page 20
Pacific School Of Engineering 219760307017

Output:

IWD Page 21
Pacific School Of Engineering 219760307017

Practical – 14
Aim: Write a script using string functions to check if the given string is loWercase or not.
Code:
<?php

$str = 'apple';

if ( ctype_lower($str) ) {

echo 'string contains only lowercase';

} else {

echo 'string does not contain only lowercase';

?>

Output:

IWD Page 22
Pacific School Of Engineering 219760307017

Practical – 15
Aim: Write a script using string functions to reverse the given string.
Code:
<?php

$string = "Shrey Khunt";

echo "Reverse string of $string is " .strrev ( $string );

?>

Output:

IWD Page 23
Pacific School Of Engineering 219760307017

Practical – 16
Aim: Write a script using string functions to remove White space from the given string.
Code:
<?php

$str = " Hello World! ";

echo "Without trim: " . $str;

echo "<br>";

echo "With trim: " . trim($str);

?>

Output:

IWD Page 24
Pacific School Of Engineering 219760307017

Practical – 17
Aim: Write a script using math functions to generate a random number betWeen the given range.
Code:
<?php

$num1 = rand();

echo "Random number: " . $num1 . "<br>";

$num2 = rand(1, 100);

echo "Random number in range (1, 100): ", $num2;

?>

Output:

IWD Page 25
Pacific School Of Engineering 219760307017

Practical – 18
Aim: Write a script using math functions to display the binary, octal & hexadecimal of a given
decimal number.

Code:
<?php

function displayNumberFormats($decimal)

echo "Decimal: $decimal <br>";

echo "Binary: " . decbin($decimal) . "<br>";

echo "Octal: " . decoct($decimal) . "<br>";

echo "Hexadecimal: " . dechex($decimal) . "<br>";

$decimalNumber = 123;

displayNumberFormats($decimalNumber);

?>

Output:

IWD Page 26
Pacific School Of Engineering 219760307017

Practical – 19
Aim: Write a script using math functions to display the sin, cos & tan of a given number.
Code:
<?php

function displayTrigFunctions($number)

$radians = deg2rad($number);

echo "Number: $number <br>";

echo "Sine: " . sin($radians) . "<br>";

echo "Cosine: " . cos($radians) . "<br>";

echo "Tangent: " . tan($radians) . "<br>";

$number = 45; // degrees

displayTrigFunctions($number);

?>

Output:

IWD Page 27
Pacific School Of Engineering 219760307017

Practical – 20
Aim: Write a script to display the current date and time in different formats.
Code:
<?php

echo "Today is " . date("Y/m/d") . "<br>";

echo "Today is " . date("Y.m.d") . "<br>";

echo "Today is " . date("Y-m-d") . "<br>";

echo "Today is " . date("l") . "<br>";

echo "Today is " . date("d-m-y h:i:s") . "<br>";

echo "Today is " . date("d");

?>

Output:

IWD Page 28
Pacific School Of Engineering 219760307017

Practical – 21
Aim: Write a script to define a class With constructor and destructor.
Code:
<?php

class MyClass {

public function construct() {

echo "Constructor called.<br>";

public function destruct() {

echo "Destructor called.<br>";

// Create an instance of MyClass

$obj = new MyClass();

?>

Output:

IWD Page 29
Pacific School Of Engineering 219760307017

Practical – 22
Aim: Write a script to create an object of a class and access its public properties and methods.
Code:
<?php

class Person {

public $name;

public $age;

public function display() {

echo "Hello, my name is {$this->name} and I am {$this->age} years old.<br>"; }}

$person = new Person();

$person->name = "Shrey Khunt";

$person->age = 17;

$person->display();

?>

Output:

IWD Page 30
Pacific School Of Engineering 219760307017

Practical – 23
Aim: Write a script that uses the set attribute and get attribute methods to access a class’s private
attributes of a class.

Code:
<?php

class MyClass {

private $privateAttribute;

public function setAttribute($value) {

$this->privateAttribute = $value;

public function getAttribute() {

return $this->privateAttribute; }

$myObject = new MyClass();

$myObject->setAttribute("Hello, World!");

echo $myObject->getAttribute();

?>

Output:

IWD Page 31
Pacific School Of Engineering 219760307017

Practical – 24
Aim: Write a script to demonstrate single inheritance.
Code:
<?php

class Base

{function BaseFun()

{echo "BaseFun() called<br>"; }

class Derived extends Base

{function DerivedFun()

{ echo "DerivedFun() called<br>"; }}

$dObj = new Derived();

$dObj->BaseFun();

$dObj->DerivedFun();

?>

Output:

IWD Page 32
Pacific School Of Engineering 219760307017

Practical – 25
Aim: Write a script to demonstrate multiple inheritance.
Code:
<?php

trait Swimmer {

public function swim() {

echo "Swimming...";}

trait Flyer {

public function fly() {

echo "Flying...";}

class Duck {use Swimmer, Flyer;}

$duck = new Duck();

$duck->swim();

$duck->fly();

?>

Output:

IWD Page 33
Pacific School Of Engineering 219760307017

Practical – 26
Aim: Write a script to demonstrate multilevel inheritance.

Code:
<?php

class Base

{function BaseFun()

{echo "BaseFun() called<br>"; }

class Derived1 extends Base

{function Derived1Fun()

{echo "Derived1Fun() called<br>";}

class Derived2 extends Derived1

{function Derived2Fun()

{echo "Derived2Fun() called<br>"; }

$dObj = new Derived2();

$dObj->BaseFun();

$dObj->Derived1Fun();

$dObj->Derived2Fun();

?>

Output:

IWD Page 34
Pacific School Of Engineering 219760307017

Practical – 27
Aim: Write a script to demonstrate method overloading.
Code:
<?php

class Calculator {

public function add(...$numbers) {

$sum = 0;

foreach ($numbers as $number) {

$sum += $number;

return $sum;}

$calc = new Calculator();

$result1 = $calc->add(5, 10);

echo "Result 1: " . $result1 . "<br>";

$result2 = $calc->add(5, 10, 15);

echo "Result 2: " . $result2 . "<br>";

?>

Output:

IWD Page 35
Pacific School Of Engineering 219760307017

Practical – 28
Aim: Write a script to demonstrate method overloading based on the number of arguments.
Code:
<?php

class Calculator {

public function add($num1, $num2, $num3 = null) {

if ($num3 !== null) {

return $num1 + $num2 + $num3;

} else {

return $num1 + $num2;

} }}

$calc = new Calculator();

$result1 = $calc->add(5, 10);

echo "Result 1: " . $result1 . "<br>";

$result2 = $calc->add(5, 10, 15);

echo "Result 2: " . $result2 . "<br>";

?>

Output:

IWD Page 36
Pacific School Of Engineering 219760307017

Practical – 29
Aim: Write a script to demonstrate a simple abstract class.

Code:
<?php

abstract class Animal {

public function construct($name) {

$this->name = $name; }

abstract public function makeSound();

class Dog extends Animal {

public function makeSound() {

return "Woof!";}}

$dog = new Dog("Buddy");

echo "Dog name: " . $dog->name . "<br>";

echo "Dog sound: " . $dog->makeSound() . "<br>";

?>

Output:

IWD Page 37
Pacific School Of Engineering 219760307017

Practical – 30
Aim: Write a script to demonstrate cloning objects.
Code:
<?php

class Person {

public $name;

public function construct($name) {

$this->name = $name;}}

$person1 = new Person("Shrey");

$person2 = clone $person1;

$person2->name = "Yogin";

print_r ($person1);

echo "<br>";

print_r ($person2);

echo "<br>";

echo "Person 1: " . $person1->name . "<br>";

echo "Person 2: " . $person2->name . "<br>";

?>

Output:

IWD Page 38
Pacific School Of Engineering 219760307017

Practical – 31
Aim: Create a web page using a form to collect employee information.
Code:
<!DOCTYPE html>

<html>

<body>

<form method="POST">

Enter EMP Name: <br> <input type="text" name="name"><br>

Enter EMP No: <br> <input type="text" name="eno"><br>

Enter Address: <br> <input type="text" name="addr"><br><br>

<button type="submit" name="submit">submit</button>

</form>

<?php

session_start();

if(isset($_POST["submit"]))

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

$_SESSION["eno"] = $_POST["eno"];

$_SESSION["name"] = $_POST["name"];

$_SESSION["addr"] = $_POST["addr"];

echo "Emp No: " . $_SESSION['eno'] . "<br>";

echo "Emp name: " . $_SESSION['name'] . "<br>";

echo "Address: " . $_SESSION['addr'] . "<br>";

session_destroy();

?>

</body>

</html>

IWD Page 39
Pacific School Of Engineering 219760307017

Output:

IWD Page 40
Pacific School Of Engineering 219760307017

Practical – 32
Aim: create a two distinct web pages to demonstrate information passing between them using
URL- Get method.

Code:
Page1.php:
<!DOCTYPE html>

<html>

<head><title>Page 1</title></head>

<body>

<h1>Page 1</h1>

<p>Enter your name:</p>

<form method="GET" action="Page2.php">

<input type="text" name="name">

<input type="submit" value="Submit">

</form>

</body>

</html>

Page2.php:
<!DOCTYPE html>

<html>

<head><title>Page 2</title></head>

<body>

<h1>Page 2</h1>

<?php

if (isset($_GET['name'])) {

$name = $_GET['name'];

echo "<p>Welcome, $name!</p>";

} else {

echo "<p>No name provided.</p>";}

IWD Page 41
Pacific School Of Engineering 219760307017

?>

<a href="Page1.php">Go back to Page 1</a>

</body>

</html>

Output:

IWD Page 42
Pacific School Of Engineering 219760307017

Practical – 33
Aim: Create two different web pages to demonstrate information passing between web pages
using Hidden variables – Post method.

Code:
Page1.php:
<!DOCTYPE html>

<html>

<head><title>Page 1</title></head>

<body>

<h1>Page 1</h1>

<p>Enter your name:</p>

<form method="POST" action="Page2.php">

<input type="text" name="name">

<input type="submit" value="Submit">

</form>

</body>

</html>

Page2.php:
<!DOCTYPE html>

<html>

<head><title>Page 2</title></head>

<body>

<h1>Page 2</h1>

<?php

if (isset($_POST['name'])) {

$name = $_POST['name'];

echo "<p>Welcome, $name!</p>";}

else {

echo "<p>No name provided.</p>"; } ?>

IWD Page 43
Pacific School Of Engineering 219760307017

<a href="Page1.php">Go back to Page 1</a>

</body>

</html>

Output:

IWD Page 44
Pacific School Of Engineering 219760307017

Practical – 34
Aim: Create web pages to demonstrate passing information using Session.
Code:
<?php

session_start();

$_SESSION['name'] = 'Shrey Khunt';

$_SESSION['time'] = time();

echo 'Your name is: ' . $_SESSION['name'];

echo '<br>';

echo 'The current time is: ' . $_SESSION['time'];

?>

Output:

IWD Page 45
Pacific School Of Engineering 219760307017

Practical – 35
Aim: Write a script to convert an associative array into JSON string format and vice versa.
Code:
<?php

$employee = [

'name' => 'Shrey Khunt',

'enroll' => 219760307017,

'department' => 'IT'

];

$jsonString = json_encode($employee);

echo "JSON String: " . $jsonString . "<br>";

$decodedArray = json_decode($jsonString, true);

echo "Decoded Array: ";

print_r($decodedArray);

?>

Output:

IWD Page 46

You might also like