0% found this document useful (0 votes)
16 views12 pages

PHP PR 4,6 (22203C0007)

The document outlines a laboratory assignment for a 6th semester Computer Engineering course focused on web-based application development using PHP. It includes various experiments such as creating and manipulating arrays, implementing functions for managing restaurant orders, and demonstrating the use of variable and anonymous functions. The document also provides code examples for each experiment and specifies the functionalities required for the assignments.

Uploaded by

adityadarekar919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views12 pages

PHP PR 4,6 (22203C0007)

The document outlines a laboratory assignment for a 6th semester Computer Engineering course focused on web-based application development using PHP. It includes various experiments such as creating and manipulating arrays, implementing functions for managing restaurant orders, and demonstrating the use of variable and anonymous functions. The document also provides code examples for each experiment and specifies the functionalities required for the assignments.

Uploaded by

adityadarekar919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

DEPARTMENT OF COMPUTER ENGINEERING

Subject: Web Based Application Subject Code: 22619


Development with PHP

Semester: 6th Semester Course: Computer Engineering

Laboratory No: L001C Name of Subject Teacher: Prof. Sneha


Patange

Name of Student: Aditya Darekar Roll Id: 22203C0007

Experiment Title of Experiment Application


No:
4 Write a PHP program for creating and 1. Write a Script to traverse indeed array,
manipulating associative array and multi-dimensional
a) Indexed array array using for and for each loop.
b) Associative array 2. Write a Script to implement array
c) Multidimensional array inbuilt functions.
6 Write a PHP program to demonstrate 3. Write a Script to manage and analyse
the use of simple function and daily orders of restaurants
parameterized function Script should include following
functionalities:
a. Add now order
b. Remove an order
c. List all the orders placed for the day
d. Identify unique order
e. Count all the orders
All should be executed with the help of
array in-built functions
4. Write a Script to implement concept of
variable function.
5. Write a Script to implement concept of
anonymous function.

Q1 Implement Associative array, indexed array, multidimensional array in


PHP

Code

<?php
$arr1 = array("Vashi", "Nerul", "Panvel", "Lower Parel");
echo "Using for loop \n";
for($i = 0; $i < count($arr1); $i++){
echo "City is : $arr1[$i] \n";
}

$arr2 = array("M416", "AMR", "AWM", "AKM", "Groza");


echo "Using foreach loop \n";
foreach($arr2 as $value){
echo "Gun is : $value \n";
}

echo "Associative array for loop\n";


$arr3 = array("AR :" => "M416", "Sniper" => "AWM", "SMG" => "MP5");

$allKeys = array_keys($arr3);
for($j = 0; $j < count($arr3); $j++){
$key = $allKeys[$j];
$value = $arr3[$key];
echo "$key : $value \n";
}

echo "Associative array foreach \n";


$arr4 = array("sem 1" => 75,"sem 2" => 80,"sem 3" => 85,"sem 4" => 90);
foreach($arr4 as $key=>$val){
echo "Marks for $key is $val \n";
}

echo "Multidimensional array for loop \n";


$arr5 = array("53" => array("Maths" => 85, "Science" => 90), "57" =>
array("Maths" => 80, "Science" => 85), "60" => array("Maths" => 10, "Science"
=> 15));
$keys = array_keys($arr5);
for ($k = 0; $k < count($arr5); $k++){
$value = $arr5[$keys[$k]];
for($l = 0; $l < count($value); $l++){
$keys2 = array_keys($value);
echo "Marks for $keys[$k] is \n";
$marks = $value[$keys2[$l]];
echo "$keys2[$l] is $marks \n";
}
echo "\n";
}

echo "Multidimesnional array foreach \n";


foreach($arr5 as $key => $value){
echo "Marks for $key is \n";
foreach($value as $key2 => $val){
$m = $value[$key2];
echo "Marks for $key2 is $m\n";
}
}
?>

Output
Q2 Implement all array functions in PHP

Code

<?php
$nums = [5, 15, 10, 20, 25];
echo "Original Array: ";
print_r($nums);
$letters = ['a', 'b', 'c', 'd', 'e'];
$combined = array_combine($letters, $nums);
echo "\nCombined Array: ";
print_r($combined);
unset($nums[2]);
echo "\nAfter Unset (Removed index 2): ";
print_r($nums);
sort($nums);
echo "\nSorted (Ascending): ";
print_r($nums);
rsort($nums);
echo "\nSorted (Descending): ";
print_r($nums);
$associative = ["x" => 12, "y" => 8, "z" => 20, "a" => 15];
asort($associative);
echo "\nAssociative Array Sorted by Value (Ascending): ";
print_r($associative);
arsort($associative);
echo "\n>Associative Array Sorted by Value (Descending): ";
print_r($associative);
ksort($associative);
echo "\nAssociative Array Sorted by Key (Ascending): ";
print_r($associative);
krsort($associative);
echo "\nAssociative Array Sorted by Key (Descending): ";
print_r($associative);
array_push($nums, 30);
array_unshift($nums, 3);
echo "\nAfter Push and Unshift: ";
print_r($nums);
array_pop($nums);
array_shift($nums);
echo "\nAfter Pop and Shift: ";
print_r($nums);
$reversed = array_reverse($nums);
echo "\nReversed Array: ";
print_r($reversed);
$sum = array_sum($nums);
echo "\nSum of Elements: $sum";
$nums2 = [10, 20, 40, 50];
$diff = array_diff($nums, $nums2);
echo "\nArray Difference: ";
print_r($diff);
$intersect = array_intersect($nums, $nums2);
echo "\nArray Intersection: ";
print_r($intersect);
$chunked = array_chunk($nums, 2);
echo "\nChunked Array: ";
print_r($chunked);
$merged = array_merge($nums, $nums2);
echo "\nMerged Array: ";
print_r($merged);
$pr = array_product($nums);
echo "\nProduct of Elements: $pr";
$search = array_search(20, $nums);
echo "\nPosition of 20: $search";
$implode = implode(", ", $nums);
echo "\nImploded Array: $implode";
$explode = explode(", ", $implode);
echo "\nExploded Array: ";
print_r($explode);
echo "\nCount of Elements: " . count($nums);
$count = array_count_values($nums);
echo "\nCount of Each Value: ";
print_r($count);
$unique = array_unique($nums);
echo "\nUnique Values: ";
print_r($unique);
?>
Output
Q3 Write a php script to manage and analyze daily orders of restaurant the
script should have following functionality's
1.Add new order
2.Remove an order
3.Get total number of orders placed
4.Display the list of orders placed
5.Identify most ordered dish

Code
<?php
$orders = [];

function placeOrder(&$orders, $dish) {


$orders[] = $dish;
echo "Order placed for: $dish\n";
}

function removeOrder(&$orders, $dish) {


foreach($orders as $i){
if($i == $dish){
unset($i);
}
}
}

function getTotalOrders(&$orders) {
return count($orders);
}

function getUniqueDishes(&$orders) {
return array_unique($orders);
}

function getMostOrderedDish(&$orders) {
$dishCount = array_count_values($orders);
if (empty($dishCount)) {
echo "No dishes ordered yet.\n";
return;
}

$maxOrders = max($dishCount);

foreach ($dishCount as $dish => $count) {


if ($count == $maxOrders) {
echo "Most ordered dish: $dish\n";
}
}
}

placeOrder($orders, "Idli");
placeOrder($orders, "Idli");
placeOrder($orders, "Tea");
removeOrder($orders, "Burger");

echo "\nOrder Summary\n";


echo "Total dishes placed: " . getTotalOrders($orders) . "\n";
echo "Dishes ordered: \n";
print_r(getUniqueDishes($orders));

echo "Most ordered dish \n";


getMostOrderedDish($orders);

?>

Output

Q4 Variable function in php

Code
<?php
function add($a1,$a2){
return $a1 + $a2;
}
$name = 'add';
echo "\nFunction call\n";
echo "Result " . $name(5,10);
?>

Output

Q5 Anonymous function in PHP

Code
<?php
$addition = function($a1,$a2){
return $a1 + $a2;
};
echo "\nFunction call\n";
echo "Result " . $addition(34,10);
?>

Output

Grade and Process Related Product Related Dated Sign


Dated (15) (10)
Signature of
Teacher

You might also like