Download File => Click
Practice Programs:
1. Write a PHP Script to define an array. Find the element from the array
that matches the given value using the appropriate search function.
<?php
$arr = array(1,2,4,5,6,7,8,9,);
$result = array_search(6, $arr);
if($result) {
print("The element is found
!!");
} else {
print("The element is not
found !!");
}
?>
========================================================
2. Write a PHP script to count the total number of vowels (a,e, i,o,u) from
the string. Show the occurrences of each vowel from the string.
<?php
$str = "Hello World";
$a_cnt = 0;
$e_cnt = 0;
$i_cnt = 0;
$o_cnt = 0;
$u_cnt = 0;
$ch = "0";
for($i = 0; $i < strlen($str);
$i++) {
$ch = strtolower($str[$i]);
switch($ch) {
case 'a' :
$a_cnt++;
break;
case 'e' :
$e_cnt++;
break;
case 'i' :
$i_cnt;
break;
case 'u' :
$u_cnt;
break;
case 'o' :
$o_cnt;
break;
}
}
echo("total number of vowels
(a,e, i,o,u) <br>");
echo("Total a count is ".
$a_cnt. "<br>");
echo("Total e count is ".
$e_cnt. "<br>");
echo("Total i count is ".
$i_cnt. "<br>");
echo("Total u count is ".
$u_cnt. "<br>");
echo("Total o count is ".
$o_cnt. "<br>");
?>
=================================================================
3. Write PHP program to perform the following operations on Indexed Array:
a) Check the array element is positive or negative
b) Calculate the average of array elements
c) Calculate the sum of array elements
<?php
$arr = [-11,2,3,4,55,6,7,80,9,-3];
$pos = 0;
$neg = 0;
foreach($arr as $value) {
if($value > 0) {
$pos++;
} else {
$neg++;
}
}
echo "1. Total Positive Or
Negative Number";
echo "<br>Total positive
number is = ".$pos;
echo "<br>Total negative
number is = ".$neg;
$sum = array_sum($arr);
$count = count($arr);
$avg = floor( $sum / $count);
echo "<BR>2. Average of
array elements is = ". $avg;
echo "<BR>3. Sum of array
elements is = ". $sum;
?>
=========================================================
SET A:
1. Write PHP program to perform the following operations on Indexed Array:
a) Union of two arrays
b) Traverse the array
elements in random order
<?php
$arr = array(12, 44, 55, 77, 22, 59);
$arr2 = array(122, 44, 556, 77,
225, 59);
$new_arr = array_diff($arr,
$arr2);
echo "Union of Two Array
<br>";
foreach($new_arr as $i) {
echo $i.", ";
}
echo "<br> Random
Order <br>";
shuffle($new_arr);
foreach($new_arr as $i) {
echo $i.", ";
}
?>
============================================================
2. Write a PHP program to perform the following operations on an
associative array:
a) Display the elements of an array along with the keys.
b) Display the size of an array
c) Delete an element from an array from the given index.
d) Reverse the order of each element’s key-value pair
e) Traverse the elements in an array in random order.
<?php
$arr = array(
"a" =>
"apple",
"b" =>
"banana",
"c" =>
"cat",
"d" =>
"dog"
);
echo "1. Array element with
the key is : <br>";
foreach($arr as $key =>
$value) {
echo $key." =>
".$value."<br>";
}
echo "2. The size of Array
is :<br>";
echo
count($arr)."<br>";
echo "3. delete element in
array <br>";
// enter the key of array
$num = "a";
unset($arr[$num]);
echo "<br> 4. Reverse
order each element key-value pair <br>";
$rev_arr = array();
foreach($arr as $key =>
$value) {
$rev_arr[$value] = $key;
}
echo "<pre>";
print_r($rev_arr);
echo "</pre>";
echo "<br>5. Random
order : <br>";
shuffle($arr);
foreach ($arr as $value) {
echo "$value
<br>";
}
?>
===========================================================
3. Write a PHP Script for the following:
a) Declare and Display a multidimensional Array.
b) Search and display a specific element from a Multidimensional array.
<!DOCTYPE html>
<html lang="en">
<head>
<meta
charset="UTF-8">
<meta
name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$student = array(
array("Ram",19),
array("Om",20),
);
$i = 0;
$len = count($student);
for ($i=0; $i < $len; $i++) {
for ($j=0; $j < $len;
$j++) {
echo $student[$i][$j];
}
echo "<br>";
}
error_reporting(0);
$search_value =
$_POST["t1"];
for ($i=0; $i < $len; $i++) {
for ($j=0; $j < $len;
$j++) {
if($search_value ==
$student[$i][$j]) {
echo "The
element is found : $search_value <br>";
}
}
echo "<br>";
}
?>
<form action="#" method="POST">
<input
type="text" name="t1" id=""
placeholder="Enter search value ">
<input
type="submit" value="Check">
</form>
</body>
</html>
==============================================================
SET B:
1. Write a PHP script to perform the following operations on string :
i) Compare string 2 with string3.
ii) Convert all the strings to Upper case
iii) Convert all the strings to Lowercase
<?php
$str1 = "Harry";
$str2 = "Ram";
$num = strcmp($str1, $str2);
if($num == 0) {
echo $str1 . $str2 .
"Are equal. <br>";
} elseif($num < 0) {
echo "The first string
is less than string two. <br>";
} else {
echo "The Second string
is less than string one <br>";
}
$c_str = strtoupper($str1);
echo
$c_str."<br>";
echo strtolower($str2);
?>
============================================================
2. Write a PHP script to perform the following operations on string :
i) Convert each word of a string to Lowercase and Uppercase.
ii) Find the first and last occurrence of string2 in string1.
<?php
$s1 = "Hello i am very
happy";
$s2 = "Ram";
echo "1.Convert each word of
a string to Lowercase and Uppercase. <br>";
$str1 = strtolower($s1);
$str2 = strtoupper($s1);
echo "lower case string :
".$str1."<br>";
echo "upper case string :
".$str2."<br>";
echo "<br>2.Find the
first and last occurrence of string2 in string1 <br>";
$string1 = "college";
$string2 = "hello";
$first = substr($string2, 0,1);
$last = substr($string2, -1);
$string1[0] = $first;
$string1[strlen($string1) -1] =
$last;
echo $string1;
?>
========================================================
3. Write a menu-driven program in PHP to perform the following operations
on associative
arrays:
i) Sort the array by values (changing the keys) in ascending, descending
order.
ii) Also, sort the array by values without changing the keys.
iii) Find the intersection of two arrays.
iv) Find the union of two arrays.
<!DOCTYPE html>
<html lang="en">
<head>
<meta
charset="UTF-8" />
<meta
name="viewport" content="width=device-width,
initial-scale=1.0" />
<title>Menu driven
program in php</title>
</head>
<body>
<div
class="container">
<form
action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<h1>Menu
Bar</h1>
<p>
1.Sort the array
by values (changing the keys) in ascending,
descending order.
</p>
<p>
2.Also, sort the
array by values without changing the keys.
</p>
<p>3.the
intersection of two arrays.</p>
<p>4.Find the
union of two arrays.</p>
<select
name="choice">
<option
value="">Enter you choice</option>
<option
value="1">1</option>
<option
value="2">2</option>
<option
value="3">3</option>
<option
value="4">4</option>
</select>
<input
type="submit" value="Show Result" />
</form>
</div>
<?php
$arr = array(
"name"
=>"Ram",
"age" =>
"19",
"s_class" =>
"SY"
);
$arr2 = array(
"name"
=>"Raj",
"age" =>
"21",
"s_class" =>
"SY"
);
if($_SERVER["REQUEST_METHOD"] == "POST") {
$ch = $_POST['choice'];
echo "<h2>
Output : </h2>";
switch ($ch) {
case 1:
asort($arr);
echo
"<h3>Ascending Order :</h3> <br>";
echo
"<pre>";
echo
print_r($arr);
echo
"</pre>";
echo
"<h3>descending Order :</h3> <br>";
arsort($arr);
echo
"<pre>";
echo
print_r($arr);
echo
"</pre>";
break;
case 2 :
echo
"2.Also, sort the array by values without changing the keys.";
asort($arr);
echo
"<pre>";
echo
print_r($arr);
echo
"</pre>";
break;
case 3:
echo "3.The
intersection of two arrays";
$result =
array_intersect_assoc($arr, $arr2);
echo
"<pre>";
echo print_r($result);
echo
"</pre>";
break;
case 4:
echo "4.Find
the union of two arrays.";
echo
"<br> Original Arrays is : <br>";
echo
"<pre>";
print_r($arr);
echo
"<br>";
print_r($arr2);
echo
"</pre>";
echo
"<br> Union of two array.";
$merge =
array_merge($arr, $arr2);
echo
"<pre>";
print_r($merge);
echo
"</pre>";
break;
default:
echo "<br> Some think is
Wrong";
}
}
?>
</body>
</html>
==============================================================
SET C:
1. Write a PHP script to perform the following operations on string :
i) Replace the string2 by string3 in string1.
ii) Reverse and display the string.
<?php
echo "1. Replace the
string2 by string3 in string1.<br>";
$string1 = "I love
php";
$string2 = "php";
$string3 = "python";
$result = str_replace($string2,
$string3, $string1);
echo $result;
echo "<br>2.Replace
the string2 by string3 in string1.<br>";
$rev_str = strrev($string1);
echo $rev_str;
?>
=============================================
0 Comments