Mann PHP Prac 2.0
Mann PHP Prac 2.0
(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.
Output:
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>
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>
Output:
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>
<?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']))
{
</body>
</html>
Output:
<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;
Output:
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>
<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>";
}
Output:
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)
{
Output:
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:
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:
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:
$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:
$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;
}
Output:
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"=>"----.",
"."=>".-.-.-",
","=>"--..--",
"?"=>"..--..",
"/"=>"-..-.",
Output:
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].":   ".$notecounter[$i]."\n";
}
}
}
countcurrency($amount);
}
Output:
$name="vraj";
if($name==strtolower($name))
{
echo "String is in lower case";
}
else
{
echo "String is not in lower case";
}
?>
Output:
$name="RMS polytechnic";
echo "String before reverse:".$name."<br>";
echo"<br>String after reverse:".strrev('RMS polytechnic');
?>
Output:
Output:
$str="RMS Polytechnic";
$replace=str_replace("Polytechnic","Campus",$str);
echo $replace;
?>
Output:
$min= "88";
$max= "90";
echo "<br>random number: ".rand($min,$max);
?>
Output:
$num="9";
echo "<br>Binary number:".decbin($num)."<br>";
echo "<br>Octal number:".decoct($num)."<br>";
echo "<br>Hexadecimal number:".dechex($num)."<br>";
?>
Output:
$num="120";
echo "<br>sin: ".sin($num);
echo "<br>cos: ".cos($num);
echo "<br>tan: ".tan($num);
?>
Output:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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>
Output:
<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'];
Output:
<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'];
Output:
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'];
Output:
$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:
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))";
if($conn->query($insert) == TRUE)
{
echo "<br>Employee Information stored successfully";
}
else
{
echo "<br>Error storing employee information:".$conn->error;
}
$conn->close();
?>
Output:
<head>
<title> Employee login </title>
</head>
<body>
<h1> Employee login </h1>
Username:
<input type="text" name="name">
Password:
<input type="password" name="pass">
</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();
?>
Output:
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>
Username:
<input type="text" name="name">
Password:
<input type="password" name="pass">
</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"];
Output:
$dbname="employee_data";
$conn=mysqli_connect("localhost","root","",$dbname);
if($conn->query($delete)===TRUE)
{
echo "Record deleted successfully";
}
else
{
echo "Error deleting record".$conn->error;
}
?>
Output:
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";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST['username'];
$old_password = $_POST['old_password'];
$new_password = $_POST['new_password'];
Output:
Old password:
New password :
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”
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 ID : </label>
<input type="text" name="em_id"><br/><br/>
<label>Allowances : </label>
<input type="text" name="allowance"><br/><br/>
<label>Deductions : </label>
<input type="text" name="deduction"><br/><br/>
</body>
</html>
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;
// 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');
?>
Output: