EX.
NO : 1 ARRAY OPERATION
Date :
AIM:
Write a PHP program to perform array operations like array search, array difference, and
array combine, array matching and sorting.
PROBLEM DESCRIPTION:
➢ Start the program.
➢ Open a new notepad window
➢ Type the array operation program on notepad
➢ Save the file as array.php
➢ Run the array.php at wamp server.
➢ Type the following address at wamp server address bar.
Local host/KT/array.php
➢ Output of array.php will be displayed at local host page.
➢ Stop the program.
1
PROGRAM
<!DOCTYPE html>
<html>
<body>
<?php
echo " ARRAY OPERATION <br/><br/>";
echo "************************* <br/>";
$a1 = array("apple","orange","mango");
$a2 = array("banana","graphes","apple");
echo "<br/><br/>";
echo " Array Search Operation <br/>";
echo".............................................<br/>";
echo "<br/><br/>";
if (in_array("apple", $a1))
echo "Search Element (Apple) is Present in the array";
else
echo "Search Element (Apple) is Not Present in the array";
echo "<br/><br/><br/><br/>";
echo " Array Difference <br />";
echo "............................ <br/>";
$result=array_diff($a2,$a1);
2
print_r($result);
echo "<br/><br/>";
echo "<br /><br/>";
echo " Array Merging <br/>";
echo "............................ <br/>";
$c=array_combine($a1,$a2);
print_r($c);
echo "<br /><br/>";
echo "<br/><br/>";
echo " Array Matching <br/>";
echo "............................ <br/>";
$match_result = array_intersect($a1,$a2);
print_r($match_result);
echo "<br /><br/>";
echo "<br/><br/>";
echo " Array Sorting <br />";
echo "............................ <br/>";
sort($a1);
foreach( $a1 as $s )
echo "$s<br />";
?>
</body>
</html>
3
OUTPUT
ARRAY OPERATION
*************************
Array Search Operation
.............................................
Search Element (Apple) is Present in the array
Array Difference
............................
Array ( [0] => banana [1] => graphes )
Array Merging
............................
Array ( [apple] => banana [orange] => graphes [mango] => apple )
Array Matching
............................
Array ( [0] => apple )
Array Sorting
............................
apple
mango
orange
4
RESULT
Thus the program executed successfully.
5
EX.NO : 2 FACTORIAL CALCULATION USING CONTROL STATEMENTS
Date :
AIM:
Write a PHP program to perform factorial calculation using if, while and do while statements.
PROBLEM DESCRIPTION:
➢ Start the program.
➢ Open a new notepad window
➢ Type the factorial program on notepad
➢ Save the file as fact.php
➢ Run the fact.php at wamp server.
➢ Type the following address at wamp server address bar.
Local host/KT/fact.php
➢ Output of fact.php will be displayed at local host page.
➢ Stop the program.
6
PROGRAM
Factorial Calculation using IF Statement
<html>
<body>
<?php
echo "FACTORIAL CALCULATION USING IF STATEMENT <br/>";
echo
"**********************************************************************</br></br>";
function fact ($n)
{
if($n <= 1)
{
return 1;
}
else
{
return $n * fact($n - 1);
}
}
echo "Factorial of 6 is " .fact(6);
?>
</body>
</html>
7
OUTPUT
FACTORIAL CALCULATION USING IF STATEMENT
*************************************************
Factorial of 6 is 720
8
PROGRAM
Factorial Calculation using While Statement
<html>
<body>
<?php
$input=5;
$fact =1;
echo "FACTORIAL CALCULATION USING WHILE STATEMENT<br/></br/>";
echo "********************************************************<br/><br/>";
for($i=$input; $i>=1;$i--) {
$fact = $fact * $i;
}
echo '<br>'. 'The factorial of the number 5 is '. $fact
?>
</body>
</html>
9
OUTPUT
FACTORIAL CALCULATION USING WHILE STATEMENT
******************************************************
The factorial of the number 5 is 120
10
PROGRAM
Factorial Calculation using Do-While Statement
<html>
<body>
<?php
echo "FACTORIAL CALCULATION USING DO-WHILE LOOP<br/>";
echo
"**********************************************************************</br></br>";
$number = 5;
$fact = 1;
do {
$fact *= $number;
$number = $number - 1;
} while ($number > 0);
echo '<br>'. 'The factorial of the number 5 is '. $fact
?>
</body>
</html>
11
OUTPUT
FACTORIAL CALCULATION USING DO-WHILE LOOP
*****************************************************
The factorial of the number 5 is 120
12
RESULT
Thus the program executed successfully.
13
EX.NO : 3 INVENTORY TABLE
Date :
AIM:
Write a PHP program to create inventory table using key and value pairs.
PROBLEM DESCRIPTION:
➢ Start the program.
➢ Open a new notepad window
➢ Type the inventory table program on notepad
➢ Save the file as inventory.php
➢ Run the inventory.php at wamp server.
➢ Type the following address at wamp server address bar.
Local host/KT/inventory.php
➢ Output of inventory.php will be displayed at local host page.
➢ Stop the program.
14
PROGRAM
<!DOCTYPE html>
<html>
<body>
<?php
echo "INVENTORY TABLE USING ASSOCIATIVE ARRAY</br>";
echo "**************************************************</br></br>";
echo "<table border=3>
<tr>
<td><font color=green>ITEM NAME</td>
<td><font color=green>QUANTITIY</td>
<td><font color=green>PRICE</td>
</tr>
<tr>
<td>Rice</td>
<td>1</td>
<td>350</td> </tr>
<td>Oil</td>
<td>1</td>
<td>150</td></tr>
<td>Soap</td>
<td>1</td>
<td>60</td></tr>
</tr>
15
</table>";
echo"</br></br>";
$item = array("Rice"=>"350", "Oil"=>"150", "Soap"=>"60");
foreach($item as $x => $x_value)
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
?>
</body>
</html>
16
OUTPUT
INVENTORY TABLE USING ASSOCIATIVE ARRAY
**************************************************
ITEM NAME QUANTITIY PRICE
Rice 1 350
Oil 1 150
Soap 1 60
Key=Rice, Value=350
Key=Oil, Value=150
Key=Soap, Value=60
17
RESULT
Thus the program executed successfully.
18
EX.NO : 4 STUDENT TABLE
Date :
AIM:
Write a PHP program to create student table using key and value pairs and search particular
student number is present or not.
PROBLEM DESCRIPTION:
➢ Start the program.
➢ Open a new notepad window
➢ Type the student table program on notepad
➢ Save the file as student.php
➢ Run the array.php at wamp server.
➢ Type the following address at wamp server address bar.
Local host/KT/student.php
➢ Output of student.php will be displayed at local host page.
➢ Stop the program.
19
PROGRAM
<!DOCTYPE html>
<html>
<body>
<?php
echo "STUDENT TABLE USING ASSOCIATIVE ARRAY FOR SEARCH A PARTICULAR
ITEM FROM ARRAY</br>";
echo
"***********************************************************************</br></br>
";
$student = array("Sharma"=>"1234",
"Poorna"=>"34322",
"Kalyan"=>"99884");
foreach($student as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
echo"</br></br>";
echo "<table border=3>
<tr>
<td><font color=green>STUDENT NAME</td>
<td><font color=green>REGISTER NUMBER</td>
<td><font color=green>MAJOR NAME</td>
<td><font color=green>YEAR</td>
</tr>
20
<tr>
<td>Sharma</td>
<td>1234</td>
<td> M.E Computer Science</td>
<td>II Year</td></tr>
<td>Poorna</td>
<td>34322</td>
<td>MBBS</td>
<td>4th Year</td></tr>
<td>Kalyan</td>
<td>99884</td>
<td>BDS</td>
<td>2nd Year</td></tr>
</tr>
</table>";
echo"</br></br>";
if (array_key_exists("Sharma",$student))
echo "Key exists!";
21
else
echo "Key does not exist!";
echo"</br></br>";
?>
</body>
</html>
22
OUTPUT
STUDENT TABLE USING ASSOCIATIVE ARRAY FOR SEARCH A PARTICULAR ITEM FROM
ARRAY
*********************************************************************************
***
Key=Sharma, Value=1234
Key=Poorna, Value=34322
Key=Kalyan, Value=99884
STUDENT NAME REGISTER NUMBER MAJOR NAME YEAR
Sharma 1234 M.E Computer Science II Year
Poorna 34322 MBBS 4th Year
Kalyan 99884 BDS 2nd Year
Key exists!
23
RESULT
Thus the program executed successfully.
24
EX.NO : 5 USER DEFINED FUNCTION TYPES
Date :
AIM:
Write a PHP program to perform user defined function.
PROBLEM DESCRIPTION:
➢ Start the program.
➢ Open a new notepad window
➢ Type the function programs on notepad
➢ Save the file with .php extension
➢ Run the file at wamp server.
➢ Type the following address at wamp server address bar.
Local host/KT/filename.php
➢ Output of file will be displayed at local host page.
➢ Stop the program.
25
5.1 NO ARGUMENT & NO RETURN VALUE WITH rand()
PROGRAM
<!DOCTYPE html>
<html>
<body>
<?php
echo "NO ARGUMENT & NO RETURN VALUE WITH rand()</br>";
echo "************************************************************</br>";
echo(rand() . "<br>");
?>
</body>
</html>
26
OUTPUT
NO ARGUMENT & NO RETURN VALUE WITH rand()
************************************************
1481545535
27
5.2 FUNCTION WITHOUT INPUT ARGUMENT AND RETURN VALUE
PROGRAM
<!DOCTYPE html>
<html>
<body>
<?php
echo "FUNCTION WITHOUT INPUT ARGUMENT AND RETURN VALUE</br>";
echo "**********************************************************</br></br>";
function pro()
$c= "welcome";
return $c;
echo pro();
?>
</body>
</html>
28
OUTPUT
FUNCTION WITHOUT INPUT ARGUMENT AND RETURN VALUE
**********************************************************
welcome
29
5.3 FUNCTION WITH ARGUMENT AND NO RETURN VALUE
PROGRAM
<!DOCTYPE html>
<html>
<body>
<?php
echo " FUNCTION WITH ARGUMENT AND NO RETURN VALUE</br>";
echo "*******************************************************</br></br>";
function get($fname) {
echo "$fname <br>";
get("Janani");
get("Honey");
get("Star");
get("Kite");
get("Biscuit");
?>
</body>
</html>
30
OUTPUT
FUNCTION WITH ARGUMENT AND NO RETURN VALUE
*******************************************************
Janani
Honey
Star
Kite
Biscuit
31
5.4 FUNCTION WITH ARGUMENT AND RETURN VALUE
PROGRAM
<!DOCTYPE html>
<html>
<body>
<?php
echo " FUNCTION WITH ARGUMENT AND RETURN VALUE</br>";
echo "*******************************************************</br></br>";
function addNumbers(int $a, int $b)
return $a + $b;
echo addNumbers(1, 5);
?>
</body>
</html>
32
OUTPUT
FUNCTION WITH ARGUMENT AND RETURN VALUE
*******************************************************
33
5.5 FUNCTION WITH DEFAULT ARGUMENT
PROGRAM
<!DOCTYPE html>
<html>
<body>
<?php
echo " FUNCTION WITH DEFAULT ARGUMENT </br>";
echo "*************************************</br></br>";
function default_fun(int $a = 50)
echo "The value of a is : $a <br>";
default_fun();
?>
</body>
</html>
34
OUTPUT
FUNCTION WITH DEFAULT ARGUMENT
*************************************
The value of a is : 50
35
RESULT
Thus the program executed successfully.
36
EX.NO : 6 FACTORIAL USING RECURSION
Date :
AIM:
Write a PHP program to perform factorial calculation using recursion.
PROBLEM DESCRIPTION:
➢ Start the program.
➢ Open a new notepad window
➢ Type the factorial calculation program on notepad
➢ Save the file as fact_recur.php
➢ Run the fact_recur.php at wamp server.
➢ Type the following address at wamp server address bar.
Local host/KT/ fact_recur.php
➢ Output of fact_recur.php will be displayed at local host page.
➢ Stop the program.
37
PROGRAM
<html>
<body>
<?php
echo "FACTORIAL CALCULATION USING RECURSION <br/>";
echo
"**********************************************************************</br></br>";
function fact ($n)
if($n <= 1)
return 1;
else
return $n * fact($n - 1);
echo "Factorial of 8 is " .fact(8);
?>
</body>
</html>
38
OUTPUT
FACTORIAL CALCULATION USING RECURSION
**********************************************
Factorial of 8 is 40320
39
RESULT
Thus the program executed successfully.
40
EX.NO : 7 NCR CALCULATION
Date :
AIM:
Write a PHP program to perform ncr calculation.
PROBLEM DESCRIPTION:
➢ Start the program.
➢ Open a new notepad window
➢ Type the ncr calcualtion program on notepad
➢ Save the file as ncr.php
➢ Run the ncr.php at wamp server.
➢ Type the following address at wamp server address bar.
Local host/KT/ncr.php
➢ Output of ncr.php will be displayed at local host page.
➢ Stop the program.
41
PROGRAM
<html>
<body>
<?php
echo "NCR CALCULATION </br>";
echo "********************************</br>";
function nCr( $n, $r)
return fact($n) / (fact($r) *
fact($n - $r));
// Returns factorial of n
function fact( $n)
$res = 1;
for ( $i = 2; $i <= $n; $i++)
$res = $res * $i;
return $res;
// Driver code
$n = 5;
$r = 3;
42
echo nCr($n, $r);
?>
</body>
</html>
43
OUTPUT
NCR CALCULATION
********************
10
44
RESULT
Thus the program executed successfully.
45
EX.NO : 8 COOKIE DEATILS
Date :
AIM:
Write a PHP program to display cookie details.
PROBLEM DESCRIPTION:
➢ Start the program.
➢ Open a new notepad window
➢ Type the cookie display program on notepad
➢ Save the file as cookie.php
➢ Run the cookie.php at wamp server.
➢ Type the following address at wamp server address bar.
Local host/KT/cookie.php
➢ Output of cookie.php will be displayed at local host page.
➢ Stop the program.
46
PROGRAM
<html>
<body>
<br>
<?php
echo " DISPLAY COOKIE DETAILS</br>";
echo "*************************************</br>";
echo "Last visited time on web page </br></br>";
$intm=60*60*24*60+time();
setcookie('lastVisit',date("G:i-d/m/y"),$intm);
if(isset($_COOKIE['lastVisit']))
$visit=$_COOKIE['lastVisit'];
echo "Your last visit was ".$visit;
else
echo "you have got same state cookies!";
?>
</body>
</html>
47
OUTPUT
DISPLAY COOKIE DETAILS
*************************************
Last visited time on web page
Your last visit was 4:49-02/05/22
48
RESULT
Thus the program executed successfully.
49
EX.NO : 9 PERSONAL DETAILS USING FILE
Date :
AIM:
Write a PHP program to display details using file concept.
PROBLEM DESCRIPTION:
➢ Start the program.
➢ Open a new notepad window
➢ Type the array operation program on notepad
➢ Save the file as array.php
➢ Run the array.php at wamp server.
➢ Type the following address at wamp server address bar.
Local host/KT/array.php
➢ Output of array.php will be displayed at local host page.
➢ Stop the program.
50
PROGRAM
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>
51
OUTPUT
52
RESULT
Thus the program executed successfully.
53
EX.NO : 10 STUDENT MARK DETAILS USING HTML FORM
Date :
AIM:
Write a PHP program to create student mark details using html form.
PROBLEM DESCRIPTION:
➢ Start the program.
➢ Open a new notepad window
➢ Type the mark program on notepad
➢ Save the file as mark.php
➢ Run the mark.php at wamp server.
➢ Type the following address at wamp server address bar.
Local host/KT/mark.php
➢ Output of mark.php will be displayed at local host page.
➢ Stop the program.
54
PROGRAM
<html>
<body>
<h3> STUDENT MARK DETAILS USING FORM </h3>
<form action ="Display.php" method="get">
<table border="5">
<tr>
<td> Enter Name:</td>
<td> <input type="text" name="name"> </td>
</tr>
<tr>
<td> Enter Student Register Number: </td>
<td> <input type="text" name="regno"></td>
</tr>
<tr>
<td> Enter Java Programming Mark: </td>
<td> <input type="text" name="m1"> </td>
</tr>
<tr>
<td> Enter System Software Mark: </td>
<td> <input type="text" name="m2"> </td>
</tr>
<tr>
<td> </td> </tr>
55
<tr> </tr>
<tr>
<td> <input type="submit" value="Submit"></td>
<td> <input type="Reset" value="Reset"></td>
</tr>
</br>
</td>
</table>
</form>
</body>
</html>
56
OUTPUT
57
RESULT
Thus the program executed successfully.
58
59