0% found this document useful (0 votes)
3 views8 pages

PHP Cookie and Array Manipulation Scripts

The document contains a series of PHP programs demonstrating various functionalities such as setting and deleting cookies, managing sessions, working with arrays, and implementing basic operations like sorting and counting elements. Each program is presented with code snippets that illustrate specific tasks, including creating associative arrays, handling multidimensional arrays, and string concatenation. Overall, it serves as a comprehensive guide for basic PHP programming techniques.
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)
3 views8 pages

PHP Cookie and Array Manipulation Scripts

The document contains a series of PHP programs demonstrating various functionalities such as setting and deleting cookies, managing sessions, working with arrays, and implementing basic operations like sorting and counting elements. Each program is presented with code snippets that illustrate specific tasks, including creating associative arrays, handling multidimensional arrays, and string concatenation. Overall, it serves as a comprehensive guide for basic PHP programming techniques.
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

Program 1: Write a PHP script to set a cookie named "username" with the value "Welcome" with an

expiration time of one hour and display its value.

<?php
$cookieName = "username";
$cookieValue = "Welcome";
$expirationTime = time() + 3600; // current time + 1 hour

setcookie($cookieName, $cookieValue, $expirationTime);

echo "Cookie named 'username' has been set with the value 'Welcome'.";
?>

Program 2: Write a PHP script to delete a cookie named "welcome".

<?php
$cookieName = "username";
setcookie($cookieName, "", time() - 3600);

echo "Cookie named 'username' has been deleted.";


?>

Program 3: Write a PHP script to retrieve and display the value of the session variable "loginid”.

<html>
<body>
<form method="post">
Enter login ID:<input type="text" name="id"><br><br>
<input type="submit" name="submit">
</form>
</body>
</html>

<?php
session_start();
if(isset($_POST['submit']))
{
$id=$_POST['id'];
$_SESSION["loginid"] = $id;
echo "<script>alert('session set');[Link]='prg3_1.php';</script>";
}
?>
Prg3_1.
<?php
session_start();
if(isset($_SESSION['loginid']))
{
$id=$_SESSION["loginid"];
echo $id;
}
?>

Program 4: Write a PHP script to check if a cookie named "visited" exists. If it does, display a welcome
message; otherwise, display a default message.

<?php
$cookieName = "visited";

if (isset($_COOKIE[$cookieName])) {
echo "Welcome back! You have visited before.";
} else {
echo "Welcome! This is your first visit.";
}

?>

Program 5: Create an array with 5 elements and print all array elements.

<?php
$array = array(1,2,3,4,5);

echo "1) using for each loop <br>";


foreach($array as $item)
{
echo $item.' ';
}
echo "<br><br>";

echo " 2) using count <br>";


$items = count($array);
for($num = 0; $num < $items; $num += 1)
{
echo $array[$num]. " ";
}
echo "<br><br>";

echo " 3) using implode <br>";


echo implode(", ", $array);

echo "<br><br>";

echo " 4) using print_r <br>";


print_r($array);
?>

Program 6: Create an array with 5 elements. Find and display minimum and maximum value from the
array.
<?php

function getMax($array)
{
$n = count($array);
$max = $array[0];
for ($i = 1; $i < $n; $i++)
if ($max < $array[$i])
$max = $array[$i];
return $max;
}

function getMin($array)
{
$n = count($array);
$min = $array[0];
for ($i = 1; $i < $n; $i++)
if ($min > $array[$i])
$min = $array[$i];
return $min;
}

$array = array(1, 2, 3, 4, 5);


echo "maximum number is:".getMax($array);
echo "<br>";
echo "minimum number is:".getMin($array);

?>
Program 7: Write a PHP program that deletes duplicate values from array.

<?php
$a = array("red","green","red","blue");
// Array after removing duplicates
print_r(array_unique($a));
?>

Program 8: rite a PHP program that checks whether the element is exists in the array or not. Give
acknowledgement from the same.

<?php
$arr = array(1, 3, 5, 6);
echo ("Original Array </br>");
echo implode(", ", $arr);
echo "<br>";

$ele = 2;

$flag = FALSE;

// Check if element exists


foreach($arr as $val)
{
if($flag==FALSE)
{
if($val == $ele)
{
$flag = TRUE;
}
}
}
if($flag ==TRUE){
print($ele . " - Element found.");
}
else{
print($ele . " - Element not found.");
}
?>
Program 9: Create an associative array and display its elements.

<?php
$person = [
"name" => "Anjali",
"age" => 23,
"city" => "New York"
];

echo $person["name"] . "\n";


echo $person["age"] . "\n";
echo $person["city"] . "\n";
?>

Program 10: Create an array named $sub, assign five elements to it and display the elements assigned
using for loop and foreach statement.

<?php
echo "<b>using for loop</b> <br>";
$sub=array("English", "Hindi", "Science","Maths","Biology");
for($i=0; $i<=4;$i++)
{
echo "$sub[$i]<br>";
}
echo "<br>";

echo "<b>using for each </b><br>";

foreach ($sub as $i)


{
echo "$i<br>";
}
?>
Program 11: Create an array named $student that stores 5 elements bounded to different keys and
access the same using the key element.
<?php
$student=array(1=>"Mr. A", 2=>"Mr. B",3=>"Mr. C",4=>"Mr. D",5=>"Mr. E");
foreach($student as $key=>$i)
{
echo "$key=>$i<br>";
}
?>
Program 12: Write a program in PHP to demonstrate the use of multidimensional arrays.

<?php
$student=array(array(1,"A"),array(2,"B"),array(3,"C"),array(4,"D"),array(5,"E"));
foreach ($student as $key=>$i)
{
echo "$key=>";
foreach($i as $j)
{
echo " $j";
}
echo "<br>";
}
?>

Program 13: Write a program in PHP to sort the array of given 5 numbers in ascending and descending
order.

<?php
$a=array(20,50,10,30,40);
for($i=0;$i<5;$i++)
{
for($j=$i;$j<5;$j++)
{
if($a[$i]>$a[$j])
{
$tmp=$a[$i];
$a[$i]=$a[$j];
$a[$j]=$tmp;
}
}
}

echo "array in asc order:<br>";


for($i=0;$i<5;$i++)
{
echo "$a[$i]<br>";
}
//desc order
for($i=0;$i<5;$i++)
{
for($j=$i;$j<5;$j++)
{
if($a[$i]<$a[$j])
{
$tmp=$a[$i];
$a[$i]=$a[$j];
$a[$j]=$tmp;
}
}
}

echo "array in desc order:<br>";


for($i=0;$i<5;$i++)
{
echo "$a[$i]<br>";
}
?>

Program 14: Write a program to count the total number of times a specific value appears in an array.

<?php
$myarray=array(1,2,3,4,5,2,2,4,2);
$valuecount=2;
$totalcount=0;
for($i=0;$i<count($myarray);$i++)
{
if($myarray[$i]==$valuecount)
{
$totalcount++;
}
}
echo "total $valuecount in array is $totalcount times."
?>
Program 15: Create two functions in PHP, parameterized and non parameterized for implementing
string concatenation operation.

<?php
echo "<b>Parameterized function</b><br>";
function joinstr($a,$b)
{
$c=$a." ".$b;
return $c;
}

$a="Good";
$b="Morning";
echo joinstr($a,$b);

echo "<br><b>Non-parameterized function</b><br>";


function joinstring()
{
$a="Good";
$b="Morning";
$c=$a." ".$b;
return $c;
}
echo joinstring();
?>

You might also like