0% found this document useful (0 votes)
83 views54 pages

PHP COMMON

The document contains 11 PHP programs with their code, output, and results. The programs cover various PHP concepts like string functions, area of circle calculation, static functions, number reversal, switch case implementation, prime numbers, palindrome checking, Fibonacci series, digit sum, Armstrong number checking, and ternary operator usage. Each program has the aim, code, output and result displayed. The programs demonstrate basic to intermediate PHP programming concepts.

Uploaded by

ABHIJITH DAS
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
83 views54 pages

PHP COMMON

The document contains 11 PHP programs with their code, output, and results. The programs cover various PHP concepts like string functions, area of circle calculation, static functions, number reversal, switch case implementation, prime numbers, palindrome checking, Fibonacci series, digit sum, Armstrong number checking, and ternary operator usage. Each program has the aim, code, output and result displayed. The programs demonstrate basic to intermediate PHP programming concepts.

Uploaded by

ABHIJITH DAS
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 54

PROGRAM NO:1

DATE:13-07-2021
STRING FUNCTION
AIM: Write a program to implement string functions in PHP.
a. strlen() b. strrev()
c. strpos() d. concatenation e. concatenation & assignment

PROGRAM CODE:
<html>
<body>
<?php
$str="WELCOME TO PHP";
echo"THE STRING IS $str";
echo"<br><br>";
echo "LENGTH OF STRING IS:".strlen($str);
echo"<br><br>";
$str1="WELCOME TO PHP";
echo "REVERSE OF $str1 IS ".strrev($str1);
echo"<br><br>";
echo"THE POSITION OF PHP IS AT: ".strpos("$str","PHP")." OF THE STRING WELCOME
TO PHP";
echo"<br><br>";
$a="WELCOME TO";
$b="PHP";
echo"A=$a";
echo"<br>";
echo"B=$b";
echo"<br><br>";
$c=$a.$b;
echo"THE CONCATENATED STRING IS:".$c;
?>
</form>
</body>
</html>

RESULT:
Program Executed Successfully

1
OUTPUT:

2
PROGRAM NO:2
DATE:13-07-2021
AREA OF CIRCLE

AIM: Program to find area of a circle

PROGRAM CODE:
<html>
<body>
<form method="post">
ENTER THE RADIUS:<input type="text" name="number">
<input type="submit" name="submit"><br>
<?php
define("pie","3.14");
$r=$_POST['number'];
$a=pie*($r*$r);
echo"<br>";
?>
AREA OF CIRCLE:<input type="text" value=<?php echo $a?>>
</form>
</body>
</html>
RESULT:
Program Executed Successfully

OUTPUT:

3
PROGRAM NO:3
DATE:13-07-2021
STATIC FUNCTION
AIM: Create a program to demonstrate working of static function

PROGRAM CODE:
<html>
<body>
<?php
class greeting
{
public static function welcome()
{
echo"HELLO WORLD!";
}
}
greeting::welcome();
?>
</form>
</body>
</html>

RESULT:
Program Executed Successfully

OUTPUT:

4
PROGRAM NO:4
DATE:13-07-2021
REVERSE OF NUMBER
AIM: Program to find reverse of a number

PROGRAM CODE:
<html>
<body>
<form method="post">
ENTER A NUMBER:<input type="text" name="number">
<input type="submit" name="submit"><br>
<?php
$n=$_POST['number'];
$r=0;
while($n>1)
{
$rem=$n%10;
$r=($r*10)+$rem;
$n=($n/10);
}
echo"REVERSE OF NUMBER IS: $r";
?>
</form>
</body>
</html>

RESULT:
Program Executed Successfully

OUTPUT:

5
PROGRAM NO:5
DATE:13-07-2021
IMPLEMENTATION OF SWITCH
AIM: Create a program that input numbers within the range of 1-12 and output related month using switch
case

PROGRAM CODE:
<html>
<body>
<form method="post">
ENTER A NUMBER FROM 1-12<input type="text" name="number">
<input type="submit" name="submit"><br>
<?php
if(isset($_POST['submit']))
{
$x=$_POST['number'];
switch($x)
{
case 1:
echo"JANUARY";
break;
case 2:
echo"FEBRUARY";
break;
case 3:
echo"MARCH";
break;
case 4:
echo "APRIL";
break;
case 5:
echo "MAY";
break;
case 6:
echo "JUNE";
break;
case 7:
echo "JULY";
break;
case 8:
echo "AUGUST";
break;
case 9:
echo "SEPTEMBER";

6
break;
case 10:
echo "OCTOBER";
break;
case 11:
echo "NOVEMBER";
break;
case 12:
echo "DECEMBER";
break;
default:
echo"ALLOWS ONLY NUMBERS FROM 1-12";
}
}
?>
</form>
</body>
</html>

RESULT:
Program Executed Successfully

OUTPUT:

7
PROGRAM NO:6
DATE:14-07-2021
PRIME NUMBERS
AIM:Find prime numbers up to a given range

PROGRAM CODE:
<html>
<body>
<form method="post">
ENTER THE LIMIT<input type="text" name="num">
<input type="submit" name="submit"><br>
<?php
if(isset($_POST['num']))
{
$n=$_POST['num'];
}
if(isset($_POST['submit']))
echo"PRIME NUMBERS:"."<br>";
for($i=2;$i<=$n;$i++)
{
for($j=2;$j<$i;$j++)
{
if($i%$j==0)
{
break;
}
}
if($j==$i)
echo $i."<br>";
}
?>
</form>
</body>
</html>

RESULT:
Program Executed Successfully

8
OUTPUT:

9
PROGRAM NO:7
DATE:14-07-2021
PALINDROME OR NOT
AIM: Check whether number palindrome or not

PROGRAM CODE:
<html>
<body>
<form method="post">
ENTER THE NUMBER:<input type="text" name="number">
<input type="submit" name="submit"><br>
<?php
if(isset($_POST['submit']))
{
$n=$_POST['number'];
$rev=strrev($n);
if($n==$rev)
{
echo"$n IS A PALINDROME";
}
else
echo"$n IS NOT A PALINDROME";
}
?>
</form>
</body>
</html>

RESULT:
Program Executed Successfully

OUTPUT:

10
PROGRAM NO:8
DATE:14-07-2021
FIBONACCI SERIES
AIM: Program to print Fibonacci series to a limit

PROGRAM CODE:
<html>
<body>
<form method="post">
ENTER YOUR LIMIT:<input type="text" name="number">
<input type="submit" name="submit"><br><br>
<?php
if(isset($_POST['submit']))
{
$x=$_POST['number'];
$num=0;
$a=0;
$b=1;
echo "fibonocci series ";

while($num<$x)
{
echo"<br>";
echo ''.$a;
$f=$a+$b;
$a=$b;
$b=$f;
$num=$num+1;
}
}

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

RESULT:
Program Executed Successfully

11
OUTPUT:

12
PROGRAM NO:9
DATE:14-07-2021
SUM OF DIGITS
AIM: Find sum of digits of a number

PROGRAM CODE:
<html>
<body>
<form method="post">
ENTER THE NUMBER<input type="text" name="number">
<input type="submit" name="submit"><br><br>
<?php
if(isset($_POST['submit']))
{
$n=$_POST['number'];
$s=0;
while($n>0)
{
$d=$n%10;
$s=$s+$d;
$n=$n/10;
}
}
echo"SUM OF DIGITS IS:".$s;
?>
</form>
</body>
</html>

RESULT:
Program Executed Successfully

OUTPUT:

13
PROGRAM NO:10
DATE:14-07-2021
AMSTRONG OR NOT
AIM: Check whether number Armstrong or not

PROGRAM CODE:
<html>
<body>
<form method="post">
ENTER THE NUMBER:<input type="text" name="number">
<input type="submit" name="submit"><br>
<?php
if(isset($_POST['submit']))
{
$num=$_POST['number'];
$res=0;
$n=$num;
while($n>0)
{
$r=$n%10;
$res=$res+($r*$r*$r);
$n=$n/10;
}
if($res==$num)
{
echo"IT IS AN AMSTRONG NUMBER";
}
else
echo"IT IS NOT AN AMSTRONG NUMBER";
}
?>
</form>
</body>
</html>

RESULT:
Program Executed Successfully

OUTPUT:

14
PROGRAM NO:11
DATE:19-07-2021
TERNARY OPERATOR
AIM: Program to test whether a number greater than 30 or not using ternary operator

PROGRAM CODE:
<html>
<body>
<form method="post">
ENTER A NUMBER<input type="text" name="num">
<input type="submit" name="submit"><br>
<?php
if(isset($_POST['num']))
{
$n=$_POST['num'];
$max=($n>30)?"THE NUMBER IS GREATER THAN 30":"THE NUMBER IS LESS THAN
30";
echo $max;
}
?>
</form>
</body>
</html>

RESULT:
Program Executed Successfully

OUTPUT:

15
PROGRAM NO:12
DATE:19-07-2021
FACTORIAL USING FUNCTION
AIM: Find factorial of a number using function

PROGRAM CODE:
<html>
<body>
<form method="post">
ENTER THE NUMBER<input type="text" name="num">
<input type="submit" name="submit"><br>
<?php
if(isset($_POST['num']))
{
$n=$_POST['num'];
$f=1;
for($x=$n;$x>=1;$x--)
{
$f=$f*$x;
}
echo"FACTORIAL OF $n IS:" .$f;
}
?>
</form>
</body>
</html>

RESULT:
Program Executed Successfully

OUTPUT:

PROGRAM NO:13
16
DATE:19-07-2021
CALL BY VALUES AND CALL BY VALUES
AIM: Program to implement call by values and call by reference in functions

PROGRAM CODE:
<html>
<body>
<?php
function add($x,&$y)
{
$x.='ASTER';
$y.='MEDICITY';
}
$a='aster';
$b='ASTER';
add($a,$b);
echo "CALL BY VALUES"."<br>";
echo $a."<br><br>";
echo "CALL BY REFERNCE"."<br>";
echo $b;
?>
</html>
</body>

RESULT:
Program Executed Successfully

OUTPUT:

PROGRAM NO:14

17
DATE:19-07-2021
GET AND POST
AIM: Write a program to create a form in PHP using: a)$_GET function b)$_POST function.

PROGRAM CODE:
A.GET METHOD:

<html>
<body>
<form method="get">
FIRST NAME:<input type="text" name="fname">
LAST NAME:<input type="text" name="Lname">
<input type="submit" name="submit"><br>
</form>
<?php
if(isset($_GET['submit']))
{
if(isset($_GET['fname']))
{
if(isset($_GET['Lname']))
{
echo "----WELCOME----"."<br>";
echo "HELLO ".$_GET['fname'],$_GET['Lname'] ;
}}}
?>
</body>
</html>

B. POST METHOD:

<html>
<body>
<form method="POST">
FIRST NAME:<input type="text" name="fname">
LAST NAME:<input type="text" name="Lname">
<input type="submit" name="submit"><br>
</form>
<?php
if(isset($_POST['submit']))
{
if(isset($_POST['fname']))
{
if(isset($_POST['Lname']))
{

18
echo "----WELCOME----"."<br>";
echo "HELLO ".$_POST['fname'], $_POST['Lname'] ;
}}}
?>
</body>
</html>

RESULT:
Program Executed Successfully

OUTPUT:
A.

B.

PROGRAM NO:15
19
DATE:19-07-2021
HELLO AND WELCOME
AIM: Write a program to create a switch statement that will output "Hello" if $color is "red", and "welcome"
if $color is "green.

PROGRAM CODE:
<html>
<body>
<form method="post">
ENTER THE COLOR<input type="text" name="color">
<input type="submit" name="submit"><br>
<?php
if(isset($_POST['color']))
{
$x=$_POST['color'];
}
if(isset($_POST['submit']))
{
switch($x)
{
case 'red':
echo "HELLO";
break;
case 'RED':
echo "HELLO";
break;
case 'green':
echo "WELCOME";
break;
case 'GREEN':
echo "WELCOME";
break;
default:
echo "ENTER EITHER RED OR GREEN";
}
}
?>
</form>
</body>
</html>

RESULT:
20
Program Executed Successfully

OUTPUT:

PROGRAM NO:16
21
DATE:19-07-2021
ILLUSTRATION OF DATE AND DATE
AIM: Program to illustrate date and time function in php

PROGRAM CODE:
<html>
<body>
<?php
echo "TODAYs DATE". date(" d/m/Y l")."<br>";
echo "TODAYs TIME ".date("h:i:s A")
?>
</body>
</html>

RESULT:
Program Executed Successfully
OUTPUT:

PROGRAM NO:17

22
DATE:20-08-2021
ARRAY FUNCTIONS
AIM: Php program to implement different array functions a) PHP array_change_key_case()
function b) PHP array_chunk() function c) PHP count() function d) PHP sort()
function e) PHP array_reverse() function f) PHP array_search() function g) PHP
array_intersect() function

PROGRAM CODE:
<html>
<body>
<?php
echo "array_chnage_key_case()";
echo "<br>";
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
print_r(array_change_key_case($age,CASE_UPPER));
echo "<br> <br>";
echo "array_chunk()";
echo"<br>";
$cars=array("VOLVO","BMW","TOYOTA","HONDA","MERCEDES","OPEL");
print_r(array_chunk($cars,2));
echo"<br><br>";
echo "count()";
echo"<br>";
$cars=array("VOLVO","BMW","TOYOTA");
echo count($cars);
echo "<br><br>";
echo"sort()";
echo "<br>";
$cars=array("VOLVO","BMW","TOYOTA");
sort($cars);
$clength=count($cars);
for($x=0;$x<$clength;$x++)
{
echo $cars[$x];
echo "<br>";
}
echo "<br><br>";
echo "array_reverse()";
echo "<br>";
$a=array("a"=>"VOLVO","b"=>"BMW","c"=>"TOYOTA");
print_r(array_reverse($a));
echo"<br><br>";
echo"array_search()";
echo"<br>";

23
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);
echo"<br>";
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_intersect($a1,$a2);
print_r($result);
?>
</body>
</html>

RESULT:
Program Executed Successfully

OUTPUT:

PROGRAM NO:18
24
DATE:10-08-2021
ILLUSTRATION OF CLASS AND OBJECT
AIM: Php script for illustrating class and object creation and setting access to member variable , functions and
this keyword.

PROGRAM CODE:
<html>
<body>
<form method="post">
ENTER THE NAME OF FRUIT<input type="text" name="name"><br>
ENTER THE COLOR<input type="text" name="color"><br>
<input type="submit" name="submit"><br>
<?php
if(isset($_POST['name'])){
$name=$_POST['name'];
}
if(isset($_POST['color'])){
$color=$_POST['color'];
}
if(isset($_POST['submit']))
{
class Fruit
{
public $name;
public $color;
function set_name($name)
{
$this->name = $name;
}
function get_name()
{
return $this->name;
}
function set_color($color)
{
$this->color = $color;
}
function get_color()
{
return $this->color;
}
}
$apple = new Fruit();
$apple->set_name($name);

25
$apple->set_color($color);
echo "NAME OF FRUIT IS " . $apple->get_name();
echo "<br>";
echo "COLOR OF FRUIT IS " . $apple->get_color();
}
?>
</body>
</html>

RESULT:
Program Executed Successfully

OUTPUT:

PROGRAM NO:19

26
DATE:10-08-2021
ILLUSTRATION OF CONSTRUCTORS AND DESTRUCTORS
AIM: Create a php program to illustrate the use of constructors and destructors

PROGRAM CODE:
<html>
<body>
<?php
class fruit
{
public $name;
public $color;
function __construct($name,$color)
{
$this->name=$name;
$this->color=$color;
}
function __destruct(){
echo"THE FRUIT IS {$this->name} AND THE COLOR IS {$this->color}.";
}
}
$apple=new fruit("APPLE","RED");
?>
<html>
<body>

RESULT:
Program Executed Successfully

OUTPUT:

PROGRAM NO:20
DATE:10-08-2021
27
INHERITANCE
AIM: Create a class car and subclass sports car.create a method for setting and printing values in parent
class.create methods in subclass also.illustrating the properties of inheritance print the output.

PROGRAM CODE:
<html>
<body>
<?php
class car
{
public $name;
public $color;
public function __construct($name,$color)
{
$this->name=$name;
$this->color=$color;
}
public function intro()
{
echo"THE CAR IS {$this->name} AND THE COLOR IS {$this->color}.";
}}
class sportscar extends car
{
public function message()
{
echo "THIS IS A SPORTSCAR<br>";
}}
$sportscar=new sportscar("VOLVO","BLACK");
$sportscar->message();
$sportscar->intro();
?>
<body>
<html>

RESULT:
Program Executed Successfully

OUTPUT:

PROGRAM NO:21
DATE:10-08-2021

28
ABSTRACT CLASS
AIM: Create a class pet with as abstract with an abstract method describe(). Create a subclass dog with
properties name ,age and color as public. Create a function initial () to set values and describe() to print
values.using the concept of abstraction in php display the results

PROGRAM CODE:
<html>
<body>
<?php
abstract class pet
{
public $name;
public $age;
public $color;
public function __construct($name,$age,$color)
{
$this->name=$name;
$this->age=$age;
$this->color=$color;
}
abstract public function intro():string;
}
class dog extends pet
{
public function intro():string{
return "THIS IS A {$this->name}"."<br>"."ITS AGE IS {$this->age}"."<br>"."AND ITS
COLOR IS {$this->color}<br><br>";
}}
class cat extends pet
{
public function intro():string{
return "THIS IS A {$this->name}"."<br>"."ITS AGE IS {$this->age}"."<br>"."AND ITS
COLOR IS {$this->color}<br><br>";
}}
class bird extends pet
{
public function intro():string{
return "THIS IS A {$this->name}"."<br>"."ITS AGE IS {$this->age}"."<br>"."AND ITS
COLOR IS {$this->color}<br><br>";
}}
$pet=new dog("DOG",4,"WHITE");
echo $pet->intro();
echo "<br>";
$pet=new cat("CAT",2,"WHITISH ORANGE");

29
echo $pet->intro();
echo "<br>";
$pet=new bird("BIRD",3,"BLUE");
echo $pet->intro();
echo "<br>";
?>
</body>
</html>

RESULT:
Program Executed Successfully

OUTPUT:

PROGRAM NO:22

30
DATE:10-08-2021
INHERITANCE
AIM: Define an interface which has methods area () and volume. Define constant pi. Create a class cylinder
which implements this interface and calculate area and volume.

PROGRAM CODE:
<html>
<body>
<form method="post">
ENTER THE RADIUS<input type="number" name="num1"><br>
ENTER THE HEIGHT<input type="number" name="num2">
<input type="submit" name="sub"><br><br>
<?php
if(isset($_POST['num1']))
{
$R=$_POST['num1'];
}
if(isset($_POST['num2']))
{
$H=$_POST['num2'];
}
if(isset($_POST['sub']))
{
interface cyl
{
function area();
function volume();
}
class cylinder implements cyl
{
public $pi=3.14;
public $a;
public $r;
public $h;
function __construct($r,$h)
{
$this->r=$r;
$this->h=$h;
}
function area()
{
$this->a=2*$this->pi*($this->h*$this->r);
echo "AREA OF CYLINDER= {$this->a}"."<br>";
}

31
function volume()
{
$this->a=$this->pi*$this->r*$this->r*$this->h;
echo "VOLUME OF CYLINDER= {$this->a}"."<br>";
}}
$c=new cylinder($R,$H);
$c->area();
$c->volume();
}
?>
</body>
</html>

RESULT:
Program Executed Successfully

OUTPUT:

PROGRAM NO:23

32
DATE:10-08-2021
MULTIPLE INTERFACE
AIM: Create a php program implementing multiple interfaces.

PROGRAM CODE:
<html>
<body>
<?php
interface Animal
{
public function sound();
}
class Cat implements Animal
{
public function sound()
{
echo "Meow ";
}}
class Dog implements Animal
{
public function sound()
{
echo "Bark ";
}}
class Mouse implements Animal
{
public function sound()
{
echo " Squeak";
}}
$cat=new Cat();
$dog=new Dog();
$mouse=new Mouse();
$animals=array($cat,$dog,$mouse);
foreach($animals as $animal)
{
$animal->sound();
}
?>
</body>
</html>

RESULT:
33
Program Executed Successfully

OUTPUT:

PROGRAM NO:24
34
DATE:10-08-2021
POLYMORPHISM
AIM: Create an interface shape with method calcarea(). Create subclass rectangle and circle and find area of
both shapes illustrating polymorphism.

PROGRAM CODE:
<html>
<body>
<form method="post">
ENTER THE RADIUS<input type="number" name="num1">
ENTER THE WIDTH<input type="number" name="num2">
ENTER THE HEIGHT<input type="number" name="num3">
<input type="submit" name="sub"><br><br>
<?php
if(isset($_POST['num1']))
{
$R=$_POST['num1'];
}
if(isset($_POST['num2']))
{
$W=$_POST['num2'];
}
if(isset($_POST['num3']))
{
$H=$_POST['num3'];
}
if(isset($_POST['sub']))
{
interface shape
{
public function calcArea();
}
class circle implements shape
{
private $r;
public function __construct($r)
{
$this->radius=$r;
}
public function calcArea()
{
return $this->radius*$this->radius*pi();
}}
class rectangle implements shape

35
{
private $width;
private $height;
public function __construct($width,$height)
{
$this->width=$width;
$this->height=$height;
}
public function calcArea()
{
return $this->width*$this->height;
}}
$circ=new circle($R);
$rect=new rectangle($W,$H);
echo "CIRCLE AREA=",$circ->calcArea();
echo "<br>";
echo "RECTANGLE AREA=",$rect->calcArea();
}
?>
</body>
</html>

RESULT:
Program Executed Successfully

OUTPUT:

PROGRAM NO:25

36
DATE:16-08-2021
UPLOAD FILE
AIM: Write a php program to upload a file

PROGRAM CODE:
<html>
<body>
<form method="post" action="file.php" enctype="multipart/form-data">
<input type="file"name="image">
<button type="submit" name="submit" >UPLOAD</button><br>
<?php
if(isset($_POST["submit"]))
{
$i=$_FILES["image"];
print_r($i);
echo "<br>";
$file_path=$i["tmp_name"];
$file_name="uploads/FILESTDNT.png";

if(move_uploaded_file($file_path,$file_name)) {
echo "FILE UPLOADED";
} else {
echo "FILE NOT UPLOADED";
}
}

RESULT:
Program Executed Successfully

OUTPUT:

PROGRAM NO:26

37
DATE:16-08-2021
COOKIES
AIM: Write a php program to implement cookies

PROGRAM CODE:
<html>
<body>
<?php
setcookie("cookie_a", "COOKIE VALUE", time() + (86400 * 30), '/');
echo $_COOKIE["cookie_a"];
setcookie("cookie_a", "COOKIE VALUE UPDATED", time() + (86400 * 30), '/');
echo "<br>" . $_COOKIE["cookie_a"];
?>
</html>
</body>

RESULT:
Program Executed Successfully
OUTPUT:

PROGRAM NO:27

38
DATE:10-08-2021
CREATE AND DESTURCTION OF SESSION
AIM: Write a php program to create & destruct session in a web page

PROGRAM CODE:
//LogIn page
<html>
<body>
<h3>LOGIN PAGE</h3>
<form method="post" action="session.php">
<input type="text" placeholder="Username" name="username"><br>
<input type="text" placeholder="Password" name="password"><br>
<button type="submit" name="submit">LOG IN</button>
<p style="color:red;"> <?php echo $error;?> </p>
<?php
session_start();
if($_SESSION["user_logged_in"]==true)
{
header("location:home.php");
}
$s_username="abd";
$s_password="abd123";
$error="";
if(isset($_POST["submit"]))
{
$in_username = $_POST["username"];
$in_password = $_POST["password"];
if($in_username==$s_username&&$in_password==$s_password)
{
$_SESSION["user_logged_in"] = true;
header("location:home.php");
}
else{
$error = "Invalid username or password";}
}
?>
</form>
</body>
</html>

//home Page
<html>
<body>
<h1>HOME PAGE</h1>

39
<form method="post" action="home.php">
<button type="submit" name="logout">LOGOUT</button>
<?php
session_start();
if($_SESSION["user_logged_in"] != true)
{
header("location:session.php");
}
if(isset($_POST["logout"]))
{
session_destroy();
header("location:session.php");
}
</form>
</body>
</html>

RESULT:
Program Executed Successfully
OUTPUT:

PROGRAM NO:28
40
DATE:10-08-2021
VALIDATION OF A FORM
AIM: create a form and validate empty field,name,number and email id

PROGRAM CODE:
<html>
<body>
<form method="post">
NAME:<input type="text" name="name"><br>
PHONE NO:<input type="text" name="num"><br>
E-MAIL:<input type="text" name="mail"><br>
<input type="submit" name="sub"><br><br>
<?php
if(isset($_POST['sub']))
{
$n=$_POST['name'];
$p=$_POST['num'];
$e=$_POST['mail'];
if(empty($n))
{
echo "NAME FIELD IS EMPTY"."<br>";}
else if(!preg_match("/^[a-zA-Z]*$/",$n)){
echo "ONLY ALPHABETS & WHITESPACES ARE ALLOWED"."<br>";}
else{echo "NAME:".$n."<br>";
}
if(empty($p))
{
echo "PHONE NO. FIELD IS EMPTY"."<br>";}
else if(!preg_match("/^[0-9]*$/",$p)){
echo "ONLY NUMERIC VALUES ARE ALLOWED"."<br>";}
else{echo "PHONE NO.:".$p."<br>";
}
if(empty($e))
{
echo "E-MAIL FIELD IS EMPTY"."<br>";}
else if(!filter_var($e,FILTER_VALIDATE_EMAIL)){
echo"E-MAIL IS NOT VALID"."<br>";}
else{echo "E-MAIL:".$e."<br>";}
}
?>
</form>
</body>
</html>

41
RESULT:
Program Executed Successfully

OUTPUT:

PROGRAM NO:29
42
Date:17/8/2021
EMPLOYEE DATABASE
AIM: write a php program to create an employee database form and include options to insert,delete,update.

PROGRAM CODE:
<html>

<body>

<form action="" method="get">

<center><b> <h2> EMPLOYEE DETAILS </h2> </b> <br></center>


Employee id : <input type="text" name="id" ?><br>
Employee name: <input type="text" name="name" /><br>
Age : <input type="text" name="age"/><br>
email : <input type="text" name="email" /><br>
Place :<input type="text" name="plc" /><br>
phone number :<input type="text" name="phn" /><br>

<input type="submit" value="insert" name ="insert"/>

<input type="submit" value="delete" name ="del"/>

<input type="submit" value="update" name ="up"/>

<input type="submit" value="view" name ="view"/> <br><br>

<?php

$servername = "localhost";

$username ="root";

$password ="aimatlab";

$dbname="empl_details";

// Create connection

$conn = mysqli_connect($servername,$username,$password,$dbname);
// Check connection if
(!$conn) {
die("Connection failed: " . mysqli_connect_error());

echo "Connection Successfull. ";

//Create database

/*

$sql = "CREATE DATABASE empl_details"; if

43
(mysqli_query($conn, $sql))
{

echo "Database created successfully";

} else

echo "Error creating database: " . mysqli_error($conn);

$sql1="CREATE TABLE employee(id VARCHAR(20) ,name VARCHAR(30)


,age VARCHAR(30), email VARCHAR(40), plc
VARCHAR(40),phn VARCHAR(30))";

if ($conn->query($sql1) === TRUE) {

echo "Table employee created successfully";

} else {

echo "Error creating table: " . $conn->error;

*/

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

$id= $_GET ["id"];

$name= $_GET ["name"];

$age= $_GET["age"];

$email= $_GET["email"];

$plc= $_GET["plc"];

$phn= $_GET["phn"];

$sql2= "INSERT INTO employee(id,name, age ,email,plc


,phn)VALUES('$id','$name', '$age' ,'$email','$plc','$phn')";

44
if ($conn->query($sql2) === TRUE)

echo "New record created successfully";

} else

echo "Error: " . $sql2 . "<br>" . $conn->error;

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

$id= $_GET ["id"];

$name= $_GET ["name"];

$age= $_GET["age"];
$email= $_GET["email"];

$plc= $_GET["plc"];

$phn= $_GET["phn"];

$sql3 = "DELETE FROM employee WHERE name='$name'";

if ($conn->query($sql3) === TRUE)

echo "Record deleted successfully";

} else

echo "Error deleting record: " . $conn->error;

45
if (isset($_GET['up']))

$id= $_GET ["id"];

$name= $_GET ["name"];

$age= $_GET["age"];

$email= $_GET["email"];

$plc= $_GET["plc"];

$phn= $_GET["phn"];

$sql4 = "UPDATE employee SET name='$name' WHERE id='$id'";

if ($conn->query($sql4) === TRUE)

echo "Record updated successfully";


}

else

echo "Error updating record: " . $conn->error;

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

$sql5 = "SELECT * FROM employee";

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

if ($result->num_rows > 0)

//output data of each row

while($row = $result->fetch_assoc())

46
{

echo "- Id: " . $row["id"]. "&nbsp &nbsp - Name: " . $row["name"]. "&nbsp
&nbsp -Age: " . $row["age"]. "&nbsp &nbsp -Email: " .
$row["email"]. "&nbsp &nbsp -Place: " . $row["plc"]. "&nbsp
&nbsp -Phone: " . $row["phn"];
echo "<br><br><br>";

} else {

echo "0 results";

}
mysqli_close($conn);

?>

</form>

</body>

</html>

RESULT:
The program was successfully executed.

47
OUTPUT:

48
49
PROGRAM NO:30
DATE:17/8/2021
STUDENT DATABASE
AIM: write a php program to create an student database form and include options to insert,delete,update.

PROGRAM CODE:
<html>
<body>
<form action="" method="get">
<center><b> <h2> STUDENT DETAILS </h2> </b> <br></center>
student id : <input type="text" name="id" ?><br>
student name: <input type="text" name="name" /><br>
Age : <input type="text" name="age"/><br>
email : <input type="text" name="email" /><br>
Place :<input type="text" name="plc" /><br>
phone number :<input type="text" name="phn" /><br>
<input type="submit" value="insert" name ="insert"/>
<input type="submit" value="delete" name ="del"/>
<input type="submit" value="update" name ="up"/>
<input type="submit" value="view" name ="view"/> <br><br>
<?php
$servername = "localhost";
$username ="root";
$password ="aimatlab";
$dbname="students_details";
// Create connection
$conn = mysqli_connect($servername,$username,$password,$dbname);
// Check connection if
(!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connection Successfull. ";
//Create database
/*$sql = "CREATE DATABASE students_details"; if
(mysqli_query($conn, $sql))
{
echo "Database created successfully";
} else
{
echo "Error creating database: " . mysqli_error($conn);
}

$sql1="CREATE TABLE student_1(id VARCHAR(20) ,name VARCHAR(30) ,age


VARCHAR(30), email VARCHAR(40), plc VARCHAR(40),phn VARCHAR(30))";

if ($conn->query($sql1) === TRUE) {

50
echo "Table student created successfully";
} else {
echo "Error creating table: " . $conn->error;
}*/
if (isset($_GET['insert']))
{
$id= $_GET ["id"];
$name= $_GET ["name"];
$age= $_GET["age"];
$email= $_GET["email"];
$plc= $_GET["plc"];
$phn= $_GET["phn"];

$sql2= "INSERT INTO student_1(id,name, age ,email,plc ,phn)VALUES('$id','$name', '$age'


,'$email','$plc','$phn')";

if ($conn->query($sql2) === TRUE)


{
echo "New record created successfully";
} else
{
echo "Error: " . $sql2 . "<br>" . $conn->error;
}
}

if (isset($_GET['del']))
{
$id= $_GET ["id"];
$name= $_GET ["name"];
$age= $_GET["age"];
$email= $_GET["email"];
$plc= $_GET["plc"];
$phn= $_GET["phn"];
$sql3 = "DELETE FROM student_1 WHERE name='$name'";

if ($conn->query($sql3) === TRUE)


{
echo "Record deleted successfully";
} else
{
echo "Error deleting record: " . $conn->error;
}
}
if (isset($_GET['up']))
{
$id= $_GET ["id"];
$name= $_GET ["name"];
$age= $_GET["age"];
$email= $_GET["email"];
$plc= $_GET["plc"];
$phn= $_GET["phn"];

51
$sql4 = "UPDATE student_1 SET name='$name' WHERE id='$id'";

if ($conn->query($sql4) === TRUE)


{
echo "Record updated successfully";
}
else
{
echo "Error updating record: " . $conn->error;
}
}

if (isset($_GET['view']))
{
$sql5 = "SELECT * FROM student_1";
$result = $conn->query($sql5);

if ($result->num_rows > 0)
{
//output data of each row
while($row = $result->fetch_assoc())
{
echo "- Id: " . $row["id"]. "&nbsp &nbsp - Name: " . $row["name"]. "&nbsp &nbsp -Age: " .
$row["age"]. "&nbsp &nbsp -Email: " . $row["email"]. "&nbsp &nbsp -Place: " . $row["plc"]. "&nbsp &nbsp
-Phone: " . $row["phn"];
echo "<br><br><br>";
}
} else {
echo "0 results";
}
}
mysqli_close($conn);
?>

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

RESULT:
The program was successfully executed.

52
OUTPUT:

53
54

You might also like