Professional Documents
Culture Documents
Practical – 1
Aim: Write a script to display Fibonacci numbers up to given term.
Code:
<html>
<head>
<title>Fibonacci</title>
</head>
<body>
<?php
$a=0;
$b=1;
echo $a;
$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>
</head>
<body>
<?php
$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>
<body>
<form method="POST">
</form>
<?php
if(isset($_POST['submit'])){
$a = $_POST['num'];
$result = $a*$i;
?>
</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'];
{ 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
</div>
</div>
<div>
</div>
<div>
</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>
</div>
<div>
</div>
</form>
<?php
if(isset($_POST['submit']))
$basic_salary=$_POST['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;
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>
<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>
</div>
</form>
<?php
if(isset($_POST['submit']))
IWD Page 8
Pacific School Of Engineering 219760307017
$com=$_POST['company'];
switch ($com)
case "Tata":
echo "it's car models are : Safari, Nexon, Tigor, Tiago <br>";
break;
case "Mahindra":
break;
case "Hyundai":
echo "it's car models are : i20, Verna, Venue, Creta <br>";
break;
case "Suzuki":
echo "it's car models are : Swift, Alto, Baleno, Brezza <br>";
break;
case "BMW":
break;
default:
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>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</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;
return 0;
else
IWD Page 12
Pacific School Of Engineering 219760307017
elseif($per<=35)
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
$i = 0;
$i--;
$word = str_word_count($str);
?>
</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
$len = strlen($str);
$word = str_word_count($str);
?>
</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);
?>
</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
{$result[$i][$j] = 0;
$result[$i][$j] += $a[$i][$k]*$b[$k][$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>
</div>
</form>
<body>
<?php
if (isset($_POST["submit"])){
$string= $_POST["string"];
$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" => "----.","." => ".-.-.-","," => "--..--","?" => "..--..","/" => "-..-
."," " =>" ");
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){
{if ($noteCounter[$i]!=0)
$amount = 2868;
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) ) {
} else {
?>
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
?>
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
echo "<br>";
?>
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();
?>
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)
$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);
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
?>
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 {
?>
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;
$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;
$this->privateAttribute = $value;
return $this->privateAttribute; }
$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()
{function DerivedFun()
$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 {
echo "Swimming...";}
trait Flyer {
echo "Flying...";}
$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()
{function Derived1Fun()
{function Derived2Fun()
$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 {
$sum = 0;
$sum += $number;
return $sum;}
?>
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 {
} else {
} }}
?>
Output:
IWD Page 36
Pacific School Of Engineering 219760307017
Practical – 29
Aim: Write a script to demonstrate a simple abstract class.
Code:
<?php
$this->name = $name; }
return "Woof!";}}
?>
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;
$this->name = $name;}}
$person2->name = "Yogin";
print_r ($person1);
echo "<br>";
print_r ($person2);
echo "<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">
</form>
<?php
session_start();
if(isset($_POST["submit"]))
$_SESSION["eno"] = $_POST["eno"];
$_SESSION["name"] = $_POST["name"];
$_SESSION["addr"] = $_POST["addr"];
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>
</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'];
} else {
IWD Page 41
Pacific School Of Engineering 219760307017
?>
</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>
</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'];
else {
IWD Page 43
Pacific School Of Engineering 219760307017
</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['time'] = time();
echo '<br>';
?>
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 = [
];
$jsonString = json_encode($employee);
print_r($decodedArray);
?>
Output:
IWD Page 46