THENI KAMMAVAR SANGAM
COLLEGE OF ARTS AND
SCIENCE
KODUVILARPATTI, THENI – 625534
APRIL-2024
PHP PROGRAMMING LAB
DEPARTMENT OF COMPUTER SCIENCE
&
INFORMATION TECHNOLOGY
THENI KAMMAVAR SANGAM COLLEGE OF
ARTS AND SCIENCE
KODUVILARPATTI, THENI – 625534
DEPARTMENT OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY
RECORD WORK
SUBJECT NAME: SUBJECT CODE:SCSJS4P
REGISTER.NO: YEAR: II-B.Sc(CS) ‘A’
This is to certify that this practical work was carried out by
___________________________ T.K.S.College of Arts and Science, Koduvilarpatti during
the academic year 2023 - 2024.
Staff – Incharge Head of the Department
Certified that this record has been submitted for the practical examination of April
2024 held on _________________.
Internal Examiner External Examiner
INDEX
PAGE.
S.NO DATE TITLE SIGN
NO
01 Array Operations
02 Control Statements
03 Inventory Tables
04 Student Table
05 User Define Function
06 Factorial using Recursion
07 nCr using command line
Display Date&Time using
08
COOKIEE
09 Personal Details using File
10 String Manipluation
Ex.No.01
ARRAY OPERATIONS
Date:
AIM:
Write a PHP Program for Array Operations (array_search
,array_difference, array_combine, array_sorting).
ALGORITHM:
Start the Program.
Declare the tag <html>,<body>.
Program Starts with <?php tag.
Declare the variables using “$” symbol.
Array Operation used
o array_search();
o array_diff();
o array_combine();
o array_sort();
Print the Result using “echo” .
Finally Close all the Tags.
Save the Program.
Run the Program.
Display the Output.
Stop the Program.
1
CODING:
ARRAY SEARCH
<html>
<body>
<?php
$birds=array('Dove','Parrot','Sparrow','Peacock','Hen');
$searchbird='Dove';
$key=array_search($searchbird,$birds);
if($key!==false)
{
echo "The Key of '$searchbird' in the array is: $key";
}
else
{
echo "The Key of '$searchbird' not Found in the array";
}
ARRAY DIFFERENCE
<html>
<body>
<?php
$array1=array('Pencil','Pen','Paper');
$array2=array('Computer','Pen','Paper');
$result=array_diff($array2,$array1);
print_r($result);
?></body>
</html>
2
ARRAY COMBINE
<html>
<body>
<?php
$Bookname=array('C++','C','Python','Java','Html');
$price=array(350,250,360,400,420);
$result=array_combine($Bookname,$price);
print_r($result);
?>
</body>
</html>
ARRAY SORTING
<html>
<body>
<?php
$a=array(28,11,10,2,20);
rsort($a);
echo"Ascending Order";
print_r($a);
?>
</body>
</html>
3
OUTPUT:
ARRAY SEARCHING
ARRAY SEARCHING The Key of 'Dove' in the array is: 0
ARRAY DIFFERENCE
Array ( [0] => Computer )
ARRAY COMBINE
Array ( [C++] => 350 [C] => 250 [Python] => 360 [Java] => 400 [Html] =>
420 )
ARRAY SORINTG
Ascending Order Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
RESULT:
Thus, the program has been Executed Successfully.
4
Ex.No.02
CONTROL STATEMENTS
Date:
AIM:
Write a PHP program to Find Factorial Value using (for, while, do-
while).
ALGORITHM:
Start the Program.
Declare the tag <html>,<body>.
Program Starts with <?php tag.
Declare the variables using “$” symbol.
Define Control Statements
o for( ){…}
o while( ){….}
o do{….}while( );
Print the Result using “echo” .
Finally Close all the Tags.
Save the Program.
Run the Program.
Display the Output.
Stop the Program.
5
CODING:
FACTOIAL USING FOR LOOP
<html>
<body>
<?php
$a=5;
$fact=1;
for($i=1;$i<=$a;$i++)
{
$fact=$fact*$i;
}
echo "Factorial of 5 is: $fact";
?>
</body>
</html>
FACTOIAL USING WHILE LOOP
<html>
<body>
<?php
$a=3;
$fact=1;
$i=1;
while($i<=$a)
{
$fact=$fact*$i;
$i++;
}
echo "Factorial of 3 is: $fact";
?>
</body>
</html>
6
FACTOIAL USING DO-WHILE LOOP
<html>
<body>
<?php
$a=6;
$fact=1;
$i=1;
do
{
$fact=$fact*$i;
$i++;
}while($i<=$a);
echo "Factorial of 6 is: $fact";
?>
</body>
</html>
7
OUTPUT:
FACTORIAL USING FOR LOOP
Factorial of 5 is: 120
FACTORIAL USING WHILE LOOP
Factorial of 3 is: 6
FACTORIAL USING DO-WHILE LOOP
Factorial of 6 is: 720
RESULT:
Thus, the program has been Executed Successfully.
8
Ex.No.03
INVENTORY TABLE USING KEY& VALUES PAIRS
Date:
AIM:
Write a PHP program to Inventory Table Using Key& Values Pairs.
ALGORITHM:
Start the Program.
Declare the tag <html>,<body>.
Program Starts with <?php tag.
Declare a Array.
Declare the while loop.
while(list($key,$value) = each($a))
{
echo "<tr>";
echo "<td> $key</td>";
echo "<td> $value</td>";
echo "</tr>";
}
Save the Program.
Run the Program.
Display the Output.
Stop the Program.
9
CODING:
<html>
<head><center>INVENTORY TABLE USING KEY& VALUES
PAIRS</center></head>
<body>
<center>
<?php
$a=array("C++"=>"500","JAVA"=>"400","PYTHON"=>"550","SQL"=>"350"
);
echo "<table border='15'>";
echo "<tr>";
echo "<th>BOOK</th>";
echo "<th>RATE</th>";
echo "</tr>";
reset($a);
while(list($key,$value) = each($a))
{
echo "<tr>";
echo "<td> $key</td>";
echo "<td> $value</td>";
echo "</tr>";
}
?>
</center>
</body>
</html>
10
OUTPUT:
RESULT:
Thus, the program has been Executed Successfully.
11
Ex.No.04
STUDENT TABLE USING KEY& VALUES PAIRS
Date:
AIM:
Write a PHP program Studen Table Using Key& Values Pairs.
ALGORITHM:
Start the Program.
Declare the tag <html>,<body>.
Program Starts with <?php tag.
Declare a Array.
Create a Student Table
Define a if loop
if(array_key_exists("GANESHKUMAR",$student))
{
echo"Key Exits!";
}
else
{
echo"<br> Key Not Exits...!!!<br>";
}
Save the Program.
Run the Program.
Display the Output.
Stop the Program.
12
CODING
<html>
<head><center>STUDENT TABLE USING KEY& VALUES
PAIRS</center></head>
<body>
<?php
$student=array("GANESHKUMAR"=>"2810","DINESHKUMAR"=>"1102","
VISHVA"=>"1234");
foreach($student as $x=>$x_value)
{
echo "Key=.$x.","Value=.$x_value";
echo "<br><br>";
}
echo "<br><br>";
echo "<table border=10>
<tr>
<td>STUDENT NAME</td>
<td>REGISTER NUMBER</td>
<td>MAJOR NAME</td>
<td>YEAR</td>
</tr>
<tr>
<td>GANESHKUMAR</td>
<td>2810</td>
<td>BE.ECE</td>
<td>FINAL YEAR</td>
</tr>
<tr>
<td>DINESHKUMAR</td>
<td>1102</td>
<td>B.Sc(CS)</td>
<td>SECOND YEAR</td>
</tr>
<tr>
<td>VISHVA</td>
<td>1234</td>
<td>B.Sc(CS)</td>
<td>SECOND YEAR</td>
</tr>
13
</table>";
echo "<br><br>";
if(array_key_exists("GANESHKUMAR",$student))
{
echo"Key Exits!";
}
else
{
echo"<br> Key Not Exits...!!!<br>";
}
?>
</body>
</html>
14
OUTPUT:
RESULT:
Thus, the program has been Executed Successfully.
15
Ex.No.05
USER DEFINE FUNCTION
Date:
AIM:
Write a PHP Program for User Define Function.
ALGORITHM:
1. Function without argument without return:
A function with no argument and is to perform a task with no value
returned.
2. Function without argument with return:
A function with no argument and is to perform a task with value
returned.
3. Function with argument withou return:
A function with argument and is to perform a task with no value
returned.
4. Function with argument return:
A function with argument and is to perform a task with value
returned.
5. Function with Default argument:
A function with Default argument.
16
CODING
Function without argument without return
<html>
<head><center>Function without argument without return</center></head>
<body>
<?php
function sum()
{
$n=50+50;
echo $n;
}
sum();
?>
</body>
</html>
Function without argument with return
<html>
<head><center>Function without argument with return</center></head>
<body>
<?php
function addnum()
{
$n1=10;
$n2=20;
$sum = $n1 + $n2;
return $sum;
}
//call function and stored returned value
$result = addnum();
echo "<br> The Sum is = $result<br>";
?>
</body>
</html>
17
Function with argument without return
<html>
<head><center>Function with argument without return</center></head>
<body>
<?php
function sum($n1,$n2)
{
$n=$n1+$n2;
echo $n;
}
sum(20,20);
?>
</body>
</html>
Function with argument with return
<html>
<head><center>Function with argument with return</center></head>
<body>
<?php
function addnum($n1,$n2)
{
$sum = $n1 + $n2;
return $sum;
}
$result= addnum(28,11);
echo "<br> The Sum is = $result<br>";
?>
</body>
</html>
18
Function with Default argument
<html>
<head><center>Function with Default Argument</center></head>
<body>
<?php
function greet($name="DINESH")
{
echo "<br> Hello,$name<br>";
}
greet();
greet("GANESH");
?>
</body>
</html>
19
OUTPUT:
Function without argument without return
The Sum of (50,50) is:100
Function without argument with return
The Sum is = 30
Function with argument without return
The Sum of (20,20) is:40
Function with argument with return
The Sum of (28,10) is:38
Function with Default argument
Hello,DINESH
Hello,GANESH
RESULT:
Thus, the program has been Executed Successfully.
20
Ex.No.06
FACTORIAL USING RECURSION
Date:
AIM:
Write a PHP Program for Factorial Using Recursion.
ALGORITHM:
Start the Program.
Declare the <html>,<body> tag.
Declare a Variables.
Declare the function factorial.
Calculate the factorial value.
Print the Result using “echo” .
Finally Close all the Tags.
Save the Program.
Run the Program.
Display the Output.
Stop the Program.
21
CODING:
<html>
<head><center>FACTORIAL USING RECURSION</center></head>
<body>
<?php
function factorial($n)
{
if($n < 0)
return -1;
else
return ($n*factorial($n-1));
}
echo “Factorial of 5 is: “.factorial(5);
?>
</body>
</html>
22
OUTPUT:
FACTORIAL USING RECURSION
Factorial of 5 is:120
RESULT:
Thus, the program has been Executed Successfully.
23
Ex.No.07
nCr CALCULATE USING INCLUDE COMMAND
Date:
AIM:
Write a PHP Program for nCr Calculate Using Include Command.
ALGORITHM:
Start the Program.
Declare the <html>,<body> tag.
Declare a include command.
Define the function nCr.
Print the Result using “echo” .
Save the Program.
Run the Program.
Display the Output.
Stop the Program.
24
CODING:
<html>
<head><center>nCr CALCULATION </center></head>
<body>
<?php
function nCr($n,$r)
{
return fact ($n)/(fact($r)*fact($n-$r));
}
function fact($n)
{
$res=1;
for($i=2;$i<=$n;$i++)
$res=$res*$i;
return $res;
}
$n=5;
$r=2;
echo nCr($n,$r);
?>
</body>
</html>
25
OUPUT:
nCr USING INCLUDE COMMAND
nCr(5,2)=10
RESULT:
Thus, the program has been Executed Successfully.
26
Ex.No.08
CURRENT DATE AND TIME USING COOKIE
Date:
AIM:
Write a PHP Program for Current Date And Time Using Cookie.
ALGORITHM:
Start the Program.
Declare the tag<html>,<body>.
Calculate the time.
$itm=60*60*24*60+time();
Print the date using setcoockie().
setcookie('last_visit',date("G:i -m/d/y"),$itm);
Define the If sttement.
Print the Result using “echo” .
Save the Program.
Run the Program.
Display the Output.
Stop the Program.
27
CODING:
<html>
<head><center>CURRENT DATE AND TIME</center></head>
<body>
<?php
$itm=60*60*24*60+time();
setcookie('last_visit',date("G:i -m/d/y"),$itm);
if(isset($_COOKIE['last_visit']))
{
$visit=$_COOKIE['last_visit'];
echo "<br> Your Last Visit is:,$visit<br>";
}
else
{
echo"<br> Youhave some stale cookies!!";
}
?>
</body>
</html>
28
OUTPUT:
CURRENT DATE AND TIME USING COOKIE
Your Last Visit is:,16:45 -03/15/24
RESULT:
Thus, the program has been Executed Successfully.
29
Ex.No.09
PERSONAL DETAILS USING FILE
Date:
AIM:
Write a PHP program for Personal Details using File concept.
ALGORITHM:
Start the Program.
Declare the tag<html>,<body>.
Create a Personal Details table.
Define a if Statement .
Save the Program.
Run the Program.
Display the Output.
Stop the Program.
30
CODING;
<html>
<head><center>PERSONAL DETAILS USING FILE</center></head>
<body>
<center>
<h1> Store Information using Files</h1>
<form method="POST" action="example.txt">
ENTER NAME:<input type=text name=t1><br><br>
ENTER REGNO:<input type=text name=t2><br><br>
ENTER COURSE:<input type=text name=t3><br><br>
ENTER PERCENTAGE:<input type=text name=t4><br><br>
<input type="submit" value="submit">
</center>
</form>
<?php
if(isset($_POST['t1']))
{
$name=$_POST['t1'];
$rno=$_POST['t2'];
$course=$_POST['t3'];
$percentage=$_POST['t4'];
$fp=fopen("example.txt","a");
$fcon="NAME:$name <br> REGNO:$rno <br> COURSE:$course
<br>PERCENTAGE:$percentage";
write($fp,$fcon);
fclose($fp);
$fp=fopen("example.txt","r");
$line=fgets($fp);
$a=explode(" ",$line);
for($i=0;$i<count($a);$i++)
{
echo "<br>";
echo $a[$i];
}
fclose($fp);
}
?>
</body>
</html>
31
OUTPUT:
RESULT:
Thus, the program has been Executed Successfully.
32
Ex.No.10
STRING MANIPULATION
Date:
AIM:
Write a PHP Program for String Manipulation.
PHP STRING FUNCTION:
1. PHP strtolower():
The strtolower() function returns string in Lowercase Letter.
2. PHP strtoupper ():
The strtoupper) function returns string in Uppercase Letter.
3. PHP ucfirst():
The ucfirst() function returns string convert first char into
Uppercase.
4. PHP lcfirst():
The lcfirst() function returns string convert first char into
Lowercase.
5. PHP ucwords():
The ucwords() function returns string convert first char of each
word into Uppercase.
6. PHP strrev():
It reverse the String.
7. PHP strlen():
It returns the length of String.
33
CODING:
<html>
<head><center>STRING FUNCTIONS (strlower)</center></head>
<body>
<?php
$s1="DINESHKUMAR";
echo"The Given String is: $s1<br>";
$s1=strtolower($s1);
echo"The Convert String is: $s1<br>";
echo"<center> STRING FUNCTIONS (strupper)</center><br>";
$s2="ganeshkumar";
echo"The Given String is: $s2<br>";
$s2=strtoupper($s2);
echo"The Convert String is: $s2<br>";
echo"<center> STRING FUNCTIONS (ucfirst)</center><br>";
$s3="ganeshkumar";
echo"The Given String is: $s3<br>";
$s3=ucfirst($s3);
echo"The Convert String is: $s3<br>";
echo"<center> STRING FUNCTIONS (lcfirst)</center><br>";
$s4="Ganeshkumar";
echo"The Given String is: $s4<br>";
$s4=lcfirst($s4);
echo"The Convert String is: $s4<br>";
echo"<center> STRING FUNCTIONS (UCWORDS)</center><br>";
$s5="my name is ganeshkumar";
echo"The Given String is: $s5<br>";
$s5=ucwords($s5);
echo"The Convert String is: $s5<br>";
34
echo"<center> STRING FUNCTIONS (strrev)</center><br>";
$s6="GANESHDINESH";
echo "The Given String is: $s6<br>";
$s6=strrev($s6);
echo"The Convert String is: $s6<br>";
echo"<center> STRING FUNCTIONS (strlen)</center><br>";
$s7="Ganeshkumar";
echo"The Given String is: $s7<br>";
$s7=strlen($s7);
echo"The Convert String is: $s7<br>";
?>
</body>
</html>
35
OUTPUT:
STRING FUNCTIONS (strlower)
The Given String is: DINESHKUMAR
The Convert String is: dineshkumar
STRING FUNCTIONS (strupper)
The Given String is: ganeshkumar
The Convert String is: GANESHKUMAR
STRING FUNCTIONS (ucfirst)
The Given String is: ganeshkumar
The Convert String is: Ganeshkumar
STRING FUNCTIONS (lcfirst)
The Given String is: Ganeshkumar
The Convert String is: ganeshkumar
STRING FUNCTIONS (UCWORDS)
The Given String is: my name is ganeshkumar
The Convert String is: My Name Is Ganeshkumar
STRING FUNCTIONS (strrev)
The Given String is: GANESHDINESH
The Convert String is: HSENIDHSENAG
STRING FUNCTIONS (strlen)
The Given String is: Ganeshkumar
The Convert String is: 11
36