0% found this document useful (0 votes)
32 views73 pages

Mann PHP Prac 2.0

Uploaded by

Mann Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views73 pages

Mann PHP Prac 2.0

Uploaded by

Mann Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 73

Introduction to web development Mann Patel

(4340704) 226420307057

Practical-1
Aim: Environment Setup
i) Install and configure PHP, Web Server and MySQL database using
XAMPP/WAMP/LAMP/MAMP.
 Steps:

Installing XAMPP
Our XAMPP tutorial will take you through the installation process for the software package on
Windows. If you’re using Linux or Mac OS X, then the steps listed below for the installation
process may differ.

Step 1: Download
XAMPP is a release made available by the non-profit project Apache Friends. Versions with PHP
5.5, 5.6, or 7 are available for download on the Apache Friends website.

Step 2: Run .exe file


Once the software bundle has been downloaded, you can start the installation by double clicking
on the file with the ending .exe.

Step 3: Deactivate any antivirus software


Since an active antivirus program can negatively affect the installation process, it’s
recommended to temporarily pause any antivirus software until all XAMPP components have
successfully been installed.

Step 4: Deactivate UAC


User Account Control (UAC) can interfere with the XAMPP installation because it limits writing
access to the C: drive, so we recommend you deactivate this too for the duration of the
installation process. To find out how to turn off your UAC, head to the Microsoft Windows support
pages.

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

Step 5: Start the setup wizard


After you’ve opened the .exe file (after deactivating your antivirus program(s) and taken note of
the User Account Control, the start screen of the XAMPP setup wizard should appear
automatically. Click on ‘Next’ to configure the installation settings.

Step 6: Choose software components


Under ‘Select Components’, you have the option to exclude individual components of the
XAMPP software bundle from the installation. But for a full local test server, we recommend you
install using the standard setup and all available components. After making your choice, click
‘Next’.

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

Step 7: Choose the installation directory


In this next step, you have the chance to choose where you’d like the XAMPP software packet to
be installed. If you opt for the standard setup, then a folder with the name XAMPP will be created
under C:\ for you. After you’ve chosen a location, click ‘Next’.

Step 8: Start the installation process


Once all the aforementioned preferences have been decided, click to start the installation. The
setup wizard will unpack and install the selected components and save them to the designated
directory. This process can take several minutes in total. You can follow the progress of this
installation by keeping an eye on the green loading bar in the middle of the screen.

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

Step 9: Windows Firewall blocking


Your Firewall may interrupt the installation process to block the some components of the
XAMPP. Use the corresponding check box to enable communication between the Apache server
and your private network or work network. Remember that making your XAMPP server available
for public networks isn’t recommended.

Step 10: Complete installation


Once all the components are unpacked and installed, you can close the setup wizard by clicking
on ‘Finish’. Click to tick the corresponding check box and open the XAMPP Control Panel once
the installation process is finished.

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

ii) Create a web page that displays “Hello World.”


 Code:
<html>
<head>
<title>practical-1</title>
</head>
<body>
<h1>This is my first PHP program</h1>
<?php
echo"hello world";
?>
</body>
</html>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

Practical-2
Aim: Form Introduction
Create a web page that collects user information using a form and displays it
when the user clicks the submit button.
 HTML code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<form style="background-color: rgb(127, 232, 255);"
<form action="insert.php" method="get">
<h1>USER INFORMATION</h1>
<p>
Name:
<input type="text" name="name:">
</p>
<p>
Collage:
<input type="text" name="collage:">
</p>
<p>
Contact:
<input type="text" name="contact:">
</p>
<p>
Email:
<input type="text" name="email:">
</p>
Branch:
<select menu="dropdown" name="branch:">
<option value="computer">computer</option>
<option value="civil">civil</option>
<option value="chemical">chemical</option>
<option value="IT">IT:</option>
</select>
<br>
Semester:
<select menu="dropdown" name="semester:">
<option value="1st">1st</option>
<option value="2nd">2nd</option>
<option value="3rd">3rd</option>

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057
<option value="4th">4th</option>
<option value="5th">5th</option>
<option value="6th">6th</option>
</select>
<br>
<input type="submit" name="btn">
</form>
</center>
</body>
</html>

 PHP code:
<html>
<body>
<?php
if(isset($_GET['btn']))
{
$name=$_GET['name:'];
$collage=$_GET['collage:'];
$contact=$_GET['contact:'];
$email=$_GET['email:'];
$branch=$_GET['branch:'];
$semester=$_GET['semester:'];

echo"<h1>Welcome $name</h1>";
echo"Collage: $collage<br>";
echo"Contact: $contact<br>";
echo"Email: $email<br>";
echo"Branch: $branch<br>";
echo"Semester: $semester<br>";

}
?>
</body>
</html>

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

Practical-3
Aim: Variables, Operators and Expressionsz
i) Write a script to implement a simple calculator for mathematical
operations.
 Code:
<html>

<body>
<form method="post">
<h1>
Number Information
</h1>
<br>
Enter Number 1:<br>
<input type="text" name="num1"><br><br>
Enter Number 2:<br>
<input type="text" name="num2"><br><br>

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


<input type="submit" name="sub" value="-">
<input type="submit" name="mul" value="*">
<input type="submit" name="div" value="/"><br><br>
<input type="reset" name="reset" value="reset"
onclick="document.getElementById('result').innerHTML='The answer is: 0';">

<?php
error_reporting(0);
$ans=0;
$add=$_POST['add'];
$sub=$_POST['sub'];
$mul=$_POST['mul'];
$div=$_POST['div'];
$num1=$_POST['num1'];
$num2=$_POST['num2'];

if(isset($_POST['add']))
{
$ans=$num1+$num2;
}
if(isset($_POST['sub']))
{
$ans=$num1-$num2;
}
if(isset($_POST['mul']))
{

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057
$ans=$num1*$num2;
}
if(isset($_POST['div']))
{
$ans=$num1/$num2;
}

echo"<h1 id='result'>The answer is:".$ans. "</h1>";


?>

</body>
</html>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

ii) A company has following payment scheme for their staff:


a. Net Salary = Gross Salary – Deduction
b. Gross Salary = Basic pay + DA + HRA + Medical
c. Deduction = Insurance + PF
Where, DA (Dearness Allowance) = 50% of Basic pay
HRA (House Rent Allowance) = 10% of Basic pay
Medical = 4% of Basic pay
Insurance = 7% of Gross salary
PF (Provident Fund) = 5% of Gross salary
Write a script to take the basic salary of an employee as input and calculate
the net payment to any employee.
 Code:
<html>

<body>
<form method="post">
<h1>
Salary Slip
</h1>
<br>
Enter Basic Salary:<br>
<input type="text" name="salary"><br><br>
<input type="submit" name="btn" value="Click for new salary">

<?php
error_reporting(0);
$base_salary=$_POST['salary'];
$da=$base_salary * 0.50;
$hra=$base_salary * 0.10;
$medical=$base_salary * 0.04;
$gross_salary=$base_salary + $da + $hra + $medical;
$insurance=$gross_salary * 0.05;
$deduction=$insurance + $pf;
$net_salary=$gross_salary - $deduction;

echo"<h1>Employee Salary Slip</h1>";


echo"<h2>Net Salary:".$net_salary."</h2>";
echo"<h2>Gross Salary:".$gross_salary."</h2>";
echo"<h2>Deduction:".$deduction."</h2>";

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057
?>
</form>
</body>
</html>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

Practical-4
Aim: Decision making statements
i) Write a script that reads the name of the car and displays the name of the
company the car belongs to as per the below table:
Car Company
Safari , Nexon , Tigor , Tiago Tata
XUV700 , XUV300 , Bolero Mahindra
i20 , Verna , Venue , Creta Hyundai
Swift , Alto , Baleno , Brezza Suzuki
 Code:
<html>

<body>
<form method="post">
<h1>
Car Information
</h1>
<br>
Enter Car Name:<br>
<input type="text" name="c_name"><br><br>

<input type="submit" name="getcompany" value="Click here for Company


Name"><br>

<br>
<?php
error_reporting(0);
$getcompany=$_POST['getcompany'];

if(isset($getcompany))
{
$car_name=$_POST['c_name'];
if($car_name=="Safari" || $car_name=="Nexon" || $car_name=="Tigor" ||
$car_name=="Tiago")
{
echo "<h1>Tata</h1>";
echo "<br><img src='tata.png' height=300 width=300>";
}
else if($car_name=="XUV700" || $car_name=="XUV300" || $car_name=="Bolaro")
{
echo "<h1>Mahindra</h1>";
echo "<br><img src='mahindra.png' height=300 width=300>";
}

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057
else if($car_name=="i20" || $car_name=="Verna" || $car_name=="Venue" ||
$car_name=="Creta")
{
echo "<h1>Hyundai</h1>";
echo "<br><img src='hyundai.png' height=300 width=300>";
}
else if($car_name=="Swift" || $car_name=="Alto" || $car_name=="Baleno" ||
$car_name=="Brezza")
{
echo "<h1>Suzuki</h1>";
echo "<br><img src='suzuki.png' height=300 width=300>";
}
else{
echo "Please enter proper car name.";
}
}
?>
</form>
</body>
</html>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

ii) Write a script to read the marks of 4 subjects and display the result as per
the below instructions:
GTU GRADE Mark-Range
AA 85-100
AB 75-84
BB 65-74
BC 55-64
CC 45-54
CD 40-44
DD 35-39
FF <35(FAIL)
a. Each of the four subjects is worth 100 marks.
b. If a student gets less than 35 marks in any subject, then he/she will be
marked as FAIL, otherwise he/she will be marked as PASS.
The result contains the grade of each individual subject in tabular format as
per the above table
 Code:
<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GTU Grade Calculator</title>
</head>
<body>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$AOOP = $_POST["AOOP"];
$ITSE = $_POST["ITSE"];
$CN = $_POST["CN"];
$ITWD = $_POST["ITWD"];
function calculateGrade($marks)
{
if ($marks >= 85)
{
return "AA";
}
elseif ($marks >= 75)
{

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057
return "AB";
}
elseif ($marks >= 65)
{
return "BB";
}
elseif ($marks >= 55)
{
return "BC";
}
elseif ($marks >= 45)
{
return "CC";
}
elseif ($marks >= 40)
{
return "CD";
}
elseif ($marks >= 35)
{
return "DD";
}
else
{
return "FF (FAIL)";
}
}

echo "<h2>GTU Grade Result</h2>";


echo "<table border='1'>";
echo "<tr><th>Subject</th><th>Marks</th><th>Grade</th></tr>";
echo "<tr><td>AOOP</td><td>$AOOP</td><td>" . calculateGrade($AOOP) .
"</td></tr>";
echo "<tr><td>ITSE</td><td>$ITSE</td><td>" . calculateGrade($ITSE) .
"</td></tr>";
echo "<tr><td>CN</td><td>$CN</td><td>" . calculateGrade($CN) .
"</td></tr>";
echo "<tr><td>ITWD</td><td>$ITWD</td><td>" . calculateGrade($ITWD) .
"</td></tr>";
echo "</table>";
}
else
{
echo "<h2>Enter Marks for 4 Subjects</h2>";
echo "<form method='post' action='" .
htmlspecialchars($_SERVER["PHP_SELF"]) . "'>";
echo "AOOP: <input type='text' name='AOOP' required><br>";
echo "ITSE: <input type='text' name='ITSE' required><br>";

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057
echo "CN: <input type='text' name='CN' required><br>";
echo "ITWD: <input type='text' name='ITWD' required><br>";
echo "<input type='submit' value='Submit'>";
echo "</form>";
}
?>
</body>
</html>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

LOOPS
iii)Write a script to display Fibonacci numbers up to a given term.
 Code:
<?php

$num=0;
$h1=0;
$h2=1;
echo"<h1>Fibonacci Series</h1>";
echo"<br>";
while($num<10)
{
$h3=$h2+$h1;
echo"$h3 <br>";
$h1=$h2;
$h2=$h3;
$num++;
}
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

iv) Write a script to display a multiplication table for the given number.
 Code:
<?php

$n=1;
echo "Table";
echo "<br>";
for($i=1;$i<=10;$i++)
{
$ans=$n*$i;
echo"$n*$i=$ans<br>";
}
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

Practical-5
Aim: Arrays
i) 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:
<?php

$str1="RMS";
$len=0;
while(@$str1[$len]!=null)
{
$len++;
}
echo "String length:$len" ;
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

ii) Write a script to sort a given indexed array.


 Code:
<?php

$number=array(4,6,2,8,7);
$arraylength=count($number);
echo "Before sorting length <br>";
for($i=0; $i<$arraylength; $i++)
{
echo $number[$i]."<br>";
}
sort ($number);
echo "After sorting length <br>";
for($i=0; $i<$arraylength; $i++)
{
echo $number[$i]."<br>";
}
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

iii) Write a script to perform 3 x 3 matrix Multiplication.


 Code:
<?php

$matrix1=array(
array(1,2,3),
array(4,5,6),
array(7,8,9)
);

$matrix2=array(
array(9,8,7),
array(6,5,4),
array(3,2,1)
);

$resultmatrix = matrix($matrix1,$matrix2);

function matrix($matrix1,$matrix2)
{
$result = array();
foreach($matrix1 as $i => $row1)
{
foreach($matrix2[0] as $j => $col12)
{
$result[$i] [$j] = 0;
foreach($row1 as $k => $value)
{
$result[$i] [$j] += $value*$matrix2[$k][$j];
}
}
}
return $result;
}

echo "Result Matrix : <br>";


foreach($resultmatrix as $row)
{
echo implode(" ",$row). "<br>";
}
?>

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

iv) Write a script to encode a given message into equivalent Morse code.
 Code:
<?php

$string = "vraj";
$string_lower = strtolower($string);
$assoc_array = 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"=>"----.",
"."=>".-.-.-",
","=>"--..--",
"?"=>"..--..",
"/"=>"-..-.",

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057
" "=>" ");
for($i=0;$i<strlen($string_lower);$i++){
foreach($assoc_array as $letter => $code){
if($letter == $string_lower[$i]){
echo "$code<br/>";
}
}
}
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

Practical-6
Aim: Functions
i) 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>P6</title>
</head>
<body>
<form method="get">
Enter amount:<input type="text" name="amount"><br>
<input type="submit" name="btn" value="submit">

<?php
if(isset($_GET['btn']))
{
$amount=$_GET['amount'];
function countcurrency($amount)
{
$notes=array(100,50,20,10,5,2,1);
$notecounter=array(0,0,0,0,0,0,0,0);
for($i=0;$i<7;$i++)
{
if($amount >= $notes[$i])
{
$notecounter[$i]=intval($amount/$notes[$i]);
$amount=$amount % $notes[$i];
}
}
for($i=0;$i<7;$i++)
{
if($notecounter[$i]!=0)
{
echo"<br>";
echo $notes[$i].":&nbsp &nbsp".$notecounter[$i]."\n";
}
}
}
countcurrency($amount);
}

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057
?>
</form>
</body>
</html>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

ii) Write scripts using string functions:


a. to check if the given string is lowercase or not.
 Code:
<?php

$name="vraj";
if($name==strtolower($name))
{
echo "String is in lower case";
}
else
{
echo "String is not in lower case";
}
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

b. to reverse the given string.


 Code:
<?php

$name="RMS polytechnic";
echo "String before reverse:".$name."<br>";
echo"<br>String after reverse:".strrev('RMS polytechnic');
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

c. to remove white spaces from the given string.


 Code:
<?php

$str=" RMS Polytechnic ";


echo "Before trim:" .$str. "<br><br>";
echo "After trim:".trim($str);
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

d. to replace the given word from the given string.


 Code:
<?php

$str="RMS Polytechnic";
$replace=str_replace("Polytechnic","Campus",$str);
echo $replace;
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

iii) Write scripts using math functions:


a. to generate a random number between the given range.
 Code:
<?php

$min= "88";
$max= "90";
echo "<br>random number: ".rand($min,$max);
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

b. to display the binary, octal and hexadecimal of a given decimal number.


 Code:
<?php

$num="9";
echo "<br>Binary number:".decbin($num)."<br>";
echo "<br>Octal number:".decoct($num)."<br>";
echo "<br>Hexadecimal number:".dechex($num)."<br>";
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

c. to display the sin, cos and tan of the given angle.


 Code:
<?php

$num="120";
echo "<br>sin: ".sin($num);
echo "<br>cos: ".cos($num);
echo "<br>tan: ".tan($num);
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

iv) Write a script to display the current date and time in different formats.
 Code:
<html>

<body>
<h2> Date & Time Formats </h2>
<?php
$current=date("y-m-d H:i:s" );
echo "<p> Format 1:".$current."</p>";
echo "<p> Format 2:".date("F j,Y,g:ia",strtotime($current))."</p>";
echo "<p> Format 3:".date("D,m,d,Y,H:i:s",strtotime($current))."</p>";
echo "<p> Format 4:".date("l,F jS,Y,g:i:sa",strtotime($current))."</p>";
?>
</body>
</html>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

Practical-7
Aim: OOP Concepts
i) Write a script to:
a. Define a class with constructor and destructor.
 Code:
<?php

class baseclass
{
public function __construct()
{
echo "This is constructor <br>";
}
public function __destruct()
{
echo "This is destructor";
}
}
$obj1=new baseclass();
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

b. Create an object of a class and access its public properties and methods.
 Code:
<?php

class student
{
public $name;
public $branch_name;
public function __construct($name,$branch_name)
{
$this->name=$name;
$this->branch_name=$branch_name;
}
public function display()
{
echo "Name: $this->name <br> Branch_name: $this->branch_name";
}
}
$obj1=new student("Vraj","Computer");
$obj1-> display();
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

ii) 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 student
{
private $name;
private $branch_name;
public function set_detail($name,$branch_name)
{
$this->name=$name;
$this->branch_name=$branch_name;
}
public function get_detail()
{
echo "Name:$this->name <br> Branch_name:$this->branch_name";
}
}
$s1=new student();
$s1->set_detail("Vraj","Computer");
$s1->get_detail();
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

iii) Write a script to demonstrate single inheritance.


 Code:
<?php

class A
{
public $name;
public $branch_name;
public $sem;
public function setdata($name,$branch_name,$sem)
{
$this->name=$name;
$this->branch_name=$branch_name;
$this->sem=$sem;
}
}
class B extends A
{
public function getdata()
{
echo"Name:$this->name <br> branch_name:$this->branch_name <br> Sem:
$this->sem";
}
}
$a1=new B();
$a1->setdata("Vraj","Computer","4");
$a1->getdata();
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

iv) Write a script to demonstrate multiple inheritance.


 Code:
<?php

class A
{
public $A;
public function setdata($A)
{
$this->a=$A;
}
}
trait B
{
public $B;
public function V2($B)
{
$this->b=$B;
}
}
class C extends A
{
use B; public $C;
public function multiplication()
{
$this->C=$this->a * $this->b;
echo "multiplication: $this->C";
}
}
$p=new C();
$p-> setdata(10);
$p-> V2(20);
$p-> multiplication();
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

v) Write a script to demonstrate multilevel inheritance.


 Code:
<?php

class A
{
public $A;
public function v1($A)
{
$this->A=$A;
}
}
class B extends A
{
public $B;
public function v2($B)
{
$this->B=$B;
}
}
class C extends B
{
public $C;
public function add()
{
$this->C=$this->A+$this->B;
echo"Addition: $this->C";
}
}
$c1=new C();
$c1->v1(10);
$c1->v2(20);
$c1->add();

?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

vi) Write a script to demonstrate method overriding.


 Code:
<?php

class collage
{
public function data()
{
echo "This is RMS Polytechnic<br>";
}
public function course()
{
echo "In RMS Campus we provide different type of course<br>";
}
}
class campus extends collage
{
public function data()
{
echo "RMS Campus provide diploma engineering,nursing and BCA
course<br>";
}
}
$c1=new campus();
$c1->data();
$c1->course();

?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

vii) Write a script to demonstrate method overloading based on the number


of arguments.
 Code:
<?php

class calculator
{
public function add()
{
$num = func_num_args();
if($num==2)
{
$arg1= func_get_arg(0);
$arg2= func_get_arg(1);
return $arg1 + $arg2;
}
else if($num==3)
{
$arg1= func_get_arg(0);
$arg2= func_get_arg(1);
$arg3= func_get_arg(2);
return $arg1 + $arg2 + $arg3;
}
else
{
return "Invalid argument";
}
}
}
$a1=new calculator();
echo "<br>".$a1->add(8,10);
echo "<br>".$a1->add(9,10,10);
echo "<br>".$a1->add(10,10,8,9);

?>
 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

viii) Write a script to demonstrate a simple interface.


 Code:
<?php

interface employee_information
{
public function emp_info();
}
class employee implements employee_information
{
public function emp_info()
{
echo "<h1>Employee Interface</h1>";
echo "My name is Vraj<br>";
echo "I am student of RMS Polytechnic";
}
}
$e1=new employee();
$e1->emp_info();

?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

ix) Write a script to demonstrate a simple abstract class.


 Code:
<?php

abstract class A
{
abstract public function d1();
abstract public function d2();
}
class B extends A
{
public function d1()
{
echo "Hello<br>";
}
public function d2()
{
echo "Good morning";
}
}
$b1=new b();
$b1->d1();
$b1->d2();

?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

x) Write a script to demonstrate cloning of objects.


 Code:
<?php

class person
{
public $name;
public $age;
public function __construct($name, $age)
{
$this->name = $name;
$this->age = $age;
}
public function __clone()
{
echo "Cloning object<br>";
}
}
$p1=new person("Vraj","18<br>");
$p2= clone($p1);
$p2->name = "Eshan";
$p2->age = "17";

echo "Object";
echo "Name:$p1->name<br> Age:$p1->age";
echo "Cloning.object<br>";
echo "Name:$p2->name<br> Age:$p2->age";
?>
 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

Practical-8
Aim: Forms
i) Create a web page using a form to collect employee information.
ii) Extend practical - 8(i) to validate user information using regular
expressions.
 Code:
<html>

<body>
<form method="post">
<h1> Employee Information </h1>

First name:
<input type="text" name="First name" id="[A-Za-z]">
<br>
Last name:
<input type="text" name="Last name" id="[A-Za-z]">
<br>
Email:
<input type="email" name="Email" id="[a-zA-Z0-9._%+-]+@[a-zA-Z0-9,-
]+\.[a-z A-z]{?}"><br>
Address:<br>
<textarea colums="2" rows="2"></textarea><br>
Mobile no:
<input type="number" name="mobile no" id="\d{10}">
<br>
DOB:
<input type="date" name="DOB"><br>
City:
<select name="city">
<option>Vadodara</option>
<option>Junagadh</option>
<option>Surat</option>
</select><br>
<input type="submit" name="submit">
</form>
</body>
</html>

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

iii) Create two distinct web pages to demonstrate information passing


between them using URL - Get method.
 Code:
<html>

<body>
<form method="get">
<h1> User Information </h1>

Name:
<input type="text" name="Name"><br>
Collage:
<input type="text" name="Collage"><br>
Branch:
<select menu="dropdown" name="Branch">
<option>Computer</option>
<option>Electrical</option>
<option>Civil</option>
<option>Chemical</option>
</select>
<br>
Sem:
<select name="Sem">
<option>2nd</option>
<option>4th</option>
<option>6th</option>
</select><br>
Email:
<input type="text" name="Email"><br>
<input type="submit" name="btn">

<?php
if(isset($_GET['btn']))
{
$Name=$_GET['Name'];
$Collage=$_GET['Collage'];
$Branch=$_GET['Branch'];
$Sem=$_GET['Sem'];
$Email=$_GET['Email'];

echo "<h1>Welcome $Name</h1>";


echo "Collage name:$Collage<br>";
echo "Sem: $Sem <br>";
echo "Branch: $Branch <br>";
echo "Email: $Email <br>";
}

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057
?>
</form>
</body>
</html>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

iv) Create two different web pages to demonstrate information passing


between web pages using Hidden variables – Post method.
 Code:
<html>

<body>
<form method="post">
<h1> User Information </h1>

Name:
<input type="text" name="Name"><br>
Collage:
<input type="text" name="Collage"><br>
Branch:
<select menu="dropdown" name="Branch">
<option>Computer</option>
<option>Electrical</option>
<option>Civil</option>
<option>Chemical</option>
</select>
<br>
Sem:
<select name="Sem">
<option>2nd</option>
<option>4th</option>
<option>6th</option>
</select><br>
Email:
<input type="text" name="Email"><br>
<input type="submit" name="btn">

<?php
if(isset($_POST['btn']))
{
$Name=$_POST['Name'];
$Collage=$_POST['Collage'];
$Branch=$_POST['Branch'];
$Sem=$_POST['Sem'];
$Email=$_POST['Email'];

echo "<h1>Welcome $Name</h1>";


echo "Collage name:$Collage<br>";
echo "Sem: $Sem <br>";
echo "Branch: $Branch <br>";
echo "Email: $Email <br>";
}

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057
?>
</form>
</body>
</html>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

Practical-9
Aim: Session, Cookies
i) Create web pages to demonstrate passing information using Session.
 HTML Code:
<html>

<body>
<h1> Session Example </h1>
<form action="session.php" method="post">
Name:
<input type="text" name="Name" id="Name">
<br>
Email:
<input type="text" name="Email" id="Email">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>

 PHP Code:
<html>

<body>
<h1> Welcome </h1>

<?php
session_start();

$_SESSION['Name']=$_POST['Name'];
$_SESSION['Email']=$_POST['Email'];

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


echo "Email:" .$_SESSION['Email']."<br>";
?>
</body>
</html>

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

ii) Write a script to demonstrate storing and retrieving information from


cookies.
 Code:
<?php

$cookie_name="User";
$cookie_value= "123";

setcookie($cookie_name,$cookie_value,time()+(86400*30),"/");

?>

<html>
<body>
<?php

if(!isset($_COOKIE[$cookie_name]))
{
echo "Cookie name".$cookie_name."is not set!";
}
else
{
echo "Cookie ". $cookie_name ." is set! <br>";
echo "Value:". $cookie_value;
}
?>
</body>
</html>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

Practical-10
Aim: Database
i) Create a web page that reads employee information using a form and
stores it in the database.
 HTML Code:
<html>

<head>
<title>Employee Information</title>
</head>
<body>
<h1>Employee Information </h1>
<form action="insert.php" method="post">

Name:
<input type="text" name="name"><br>
Email:
<input type="text" name="email"><br>
Department:
<input type="text" name="department"><br>
<input type="submit" name="submit">

</form>
</body>
</html>

 PHP Code:
<?php

$dbname="employee_information";
$conn=mysqli_connect("localhost","root","",$dbname);

if($conn)
{
echo "Connection successful";
}
else
{
echo "not connected";
}
$sql="create table employee (name varchar(20)not null,email
varchar(20) not null,department varchar(20))";

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057
if ($conn->query($sql) == TRUE)
{
echo "<br>Table created successfully";
}
else
{
echo "<br>Error in creating table:".mysqli_error($sql);
}
$name=$_POST['name'];
$email=$_POST['email'];
$department=$_POST['department'];

$insert="insert into employee (name,email,department)


value('$name','$email','$department')";

if($conn->query($insert) == TRUE)
{
echo "<br>Employee Information stored successfully";
}
else
{
echo "<br>Error storing employee information:".$conn->error;
}
$conn->close();
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

ii) Create a web page for employee log-in.


 HTML Code:
<html>

<head>
<title> Employee login </title>
</head>
<body>
<h1> Employee login </h1>

<form action="connection.php" method="post">

Username:
<input type="text" name="name">
Password:
<input type="password" name="pass">

<input type="submit" name="submit">

</html>

 PHP Code:
<?php

$dbname="employee_data";
$name=$_POST['name'];
$password=$_POST['pass'];

$conn=mysqli_connect("localhost","root","",$dbname);
$sql="select * from employee_login WHERE username='$name' AND
password='$password'";

$result=$conn->query($sql);

if($result->num_rows==1)
{
echo "<br>Login successful";
}
else
{
echo "<br>invalid username or password";
}

$conn->close();
?>

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

iv) After an employee logs in, create a Home web page that displays basic
employee information.
 HTML Code:
<html>

<head>
<title> Employee login </title>
</head>
<body>
<h1> Employee login </h1>

<form action="info.php" method="post">

Username:
<input type="text" name="name">
Password:
<input type="password" name="pass">

<input type="submit" name="submit">

</html>

 PHP Code:
<?php

$dbname="employee_data";
$name=$_POST['name'];
$password=$_POST['pass'];

$conn=mysqli_connect("localhost","root","",$dbname);
$sql="select * from employee_login WHERE username='$name' AND
password='$password'";

$result=$conn->query($sql);

if($result->num_rows==1)
{
echo "<br> Login Successful <br>";
}
$sql1="select * from employee where id=1";
$result1=$conn->query($sql1);
if($result1->num_rows>0)
{
while($row=$result1->fetch_assoc())
{
$column1value=$row["Name"];

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057
$column2value=$row["Email"];
$column3value=$row["Department"];
echo "<br> Employee Information <br>";
echo "<br>";
echo "Name:".$column1value."<br>Email:".
$column2value."<br>Department:".$column3value."<br>";
}
}
else
{
echo "No result found";
}
$conn->close();
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

v) Create a web page to delete employee profiles from the database.


 Code:
<?php

$dbname="employee_data";
$conn=mysqli_connect("localhost","root","",$dbname);

$delete="delete from employee where id=1";

if($conn->query($delete)===TRUE)
{
echo "Record deleted successfully";
}
else
{
echo "Error deleting record".$conn->error;
}
?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

vi) Create a web page that allows employees to change their password.
 HTML Code:
<html>
<head>
<title>Password Change</title>
</head>
<body>
<h2>Password Change</h2>
<form action="password.php" method="post">
Username: <input type="text" name="username"><br><br>
Old Password: <input type="password" name="old_password"><br><br>
New Password: <input type="password" name="new_password"><br><br>
<input type="submit" name="submit" value="Change Password">
</form>
</body>
</html>

 PHP Code:
<?php

$dbname="employee_data";

$conn = new mysqli("localhost", "root","", $dbname);


if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST['username'];
$old_password = $_POST['old_password'];
$new_password = $_POST['new_password'];

$sql = "SELECT * FROM employee_login WHERE username='$username' AND


password='$old_password'";
$result =$conn->query($sql);
if($result->num_rows > 0)
{
$sql = "UPDATE employee_login SET password='$new_password' WHERE
username='$username'";
if ($conn->query($sql) === TRUE)
{
echo "Password updated successfully!";
}

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057
else
{
echo "Error updating password: " . $conn->error;
}
}
else
{
echo "Invalid username or old password!";
}
}
?>

 Output:

 Old password:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

 New password :

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

Practical-11
Aim:
i) Write a script to generate a salary slip for an employee in PDF format.
 Steps:
1. First click on following link http://www.fpdf.org/
2. Download the zip file by clicking “there”

3. After downloading, create one folder in htdocs for fpdf, extract


this downloaded zip by clicking on "extract to fpdf186" here in
this fpdf folder.
Now place these two php files index1.php and generate_pdf.php
here in this htdocs folder only.

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

 Code:
 Index1.php
<!DOCTYPE html>

<html>
<head>
<title>Download PDF Example</title>
</head>
<body>
<form action="generate_pdf.php" method="post">
<h2>Click below to download PDF</h2>

<label>Employee Name : </label>


<input type="text" name="em_name"><br/><br/>

<label>Employee ID : </label>
<input type="text" name="em_id"><br/><br/>

<label>Basic Salary : </label>


<input type="text" name="basic"><br/><br/>

<label>Allowances : </label>
<input type="text" name="allowance"><br/><br/>

<label>Deductions : </label>
<input type="text" name="deduction"><br/><br/>

<a href="generate_pdf.php"><input type="submit" value="Submit"></a>


</form>

</body>
</html>

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

 generate_pdf.php
<?php

require('fpdf186/fpdf.php');

// Employee details
$employeeName = $_POST['em_name'];
$employeeID = $_POST['em_id'];
$basicSalary = $_POST['basic'];
$allowances = $_POST['allowance'];
$deductions = $_POST['deduction'];
$totalSalary = $basicSalary + $allowances - $deductions;

// Create a new FPDF instance


$pdf = new FPDF();
$pdf->AddPage();

// Set font
$pdf->SetFont('Arial', 'B', 16);

// Title
$pdf->Cell(0, 10, 'Salary Slip', 0, 1, 'C');
$pdf->Ln(10);

// Employee details
$pdf->SetFont('Arial', '', 12);
$pdf->Cell(0, 10, 'Employee Name: ' . $employeeName, 0, 1);
$pdf->Cell(0, 10, 'Employee ID: ' . $employeeID, 0, 1);
$pdf->Ln(5);

// Salary details
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(60, 10, 'Description', 1, 0, 'C');
$pdf->Cell(40, 10, 'Amount (USD)', 1, 1, 'C');

$pdf->SetFont('Arial', '', 12);


$pdf->Cell(60, 10, 'Basic Salary', 1, 0);
$pdf->Cell(40, 10, $basicSalary, 1, 1);

$pdf->Cell(60, 10, 'Allowances', 1, 0);


$pdf->Cell(40, 10, $allowances, 1, 1);

$pdf->Cell(60, 10, 'Deductions', 1, 0);


$pdf->Cell(40, 10, $deductions, 1, 1);

$pdf->SetFont('Arial', 'B', 12);


$pdf->Cell(60, 10, 'Total Salary', 1, 0);
$pdf->Cell(40, 10, $totalSalary, 1, 1);

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

// Output the PDF


$pdf->Output('salary_slip.pdf', 'D');

// Set headers for PDF download


header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="salary_slip.pdf"');
header('Content-Length: ' . strlen($pdfContent));

// Output the PDF


echo $pdfContent;

?>

 Output:

Page.No: _____ Sign: __________


Introduction to web development Mann Patel
(4340704) 226420307057

Page.No: _____ Sign: __________

You might also like