PHP Record - Final
PHP Record - Final
APRIL-2024
RECORD WORK
Certified that this record has been submitted for the practical examination of April
2024 held on _________________.
01 Array Operations
02 Control Statements
03 Inventory Tables
04 Student Table
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:
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:
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>
<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:
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:
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:
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:
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();
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);
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:
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:
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:
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:
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:
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:
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:
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>";
$s2="ganeshkumar";
echo"The Given String is: $s2<br>";
$s2=strtoupper($s2);
echo"The Convert String is: $s2<br>";
$s3="ganeshkumar";
echo"The Given String is: $s3<br>";
$s3=ucfirst($s3);
echo"The Convert String is: $s3<br>";
$s4="Ganeshkumar";
echo"The Given String is: $s4<br>";
$s4=lcfirst($s4);
echo"The Convert String is: $s4<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>";
$s7="Ganeshkumar";
echo"The Given String is: $s7<br>";
$s7=strlen($s7);
echo"The Convert String is: $s7<br>";
?>
</body>
</html>
35
OUTPUT:
36