3739 Assignment 2
3739 Assignment 2
ROLL NO-(3739)
QUESTION 1) $color = array('white', 'green', 'red', 'blue', 'black');
Write a script which will display the following string -
"The memory of that scene for me is like a frame of film forever frozen at that moment: the red
carpet, the green lawn, the white house, the leaden sky. The new president and his first lady. -
Richard M. Nixon"
and the words 'red', 'green' and 'white' will come from $color.
Answer 1)
INPUT:
<?php
$color=array('white','green','red','blue','black');
echo "The memory of that scene for me is like a frame of film forever frozen at that moment: the
$color[2] carpet, the $color[1] lawn, the $color[0] house, the leaden sky. The new president and his
first lady.-Richard M. Nixon”. “\n”
?>
OUTPUT:
The memory of that scene for me is like a frame of film forever frozen at that moment: the red
carpet, the green lawn, the white house, the leaden sky. The new president and his first lady. -
Richard M. Nixon
Answer 2)
INPUT:
<?php
echo reset($color)."\n";
?>
OUTPUT: white
QUESTION 3) Write a PHP function to compare two multidimensional arrays and returns the
difference.
ANSWER 3)
INPUT:
<?php
function key_compare($a,$b)
{
if($a===$b)
return 0;
return($a>$b)?1:-1;
//multidimenssional arrays
$color1=array('a'=>'White','b'=>'Red','c'=>array('a'=>'Green','b'=>'Blue','c'=>'Yellow'));
$color2=array('a'=>'White','b'=>'Red','c'=>array('a'=>'White','b'=>'Red','c'=>'Yellow'));
print_r(multidimenssional_array_diff($color1, $color2));
?>
OUTPUT:
ANSWER 4)
INPUT:
<?php
$my_array = array(15, null, " ", -2, NULL, "", " \n", "Red", 54, "\t");
print_r($my_array);
print_r($result);
?>
OUTPUT:
Array ( [0] => 15 [1] => [2] => [3] => -2 [4] => [5] => [6] => [7] => Red [8] => 54 [9] => ) Array ( [0] =>
15 [3] => -2 [7] => Red [8] => 54 )
QUESTION 5)Write a PHP program to filter out some elements with certain key-names.
Test Data :
1st array : ('c1' => 'Red', 'c2' => 'Green', 'c3' => 'White', c4 => 'Black')
2nd array : ('c2', 'c4')
Output :
Array
(
[c1] => Red
[c3] => White
)
INPUT:
<?php
$first_array = array('c1' => 'Red', 'c2' => 'Green', 'c3' => 'White', 'c4' => 'Black');
print_r($result);
?>
OUTPUT:
QUESTION 6)Write a PHP function to sort an array according to another array acting as a priority list.
ANSWER 6)
INPUT:
<?php
global $order;
if($a==$value)
return 0;
break;
if($b==$value)
{
return 1;
break;
$order[0] = 1;
$order[1] = 3;
$order[2] = 4;
$order[3] = 2;
$array[0] = 2;
$array[1] = 1;
$array[2] = 3;
$array[3] = 4;
$array[4] = 2;
$array[5] = 1;
$array[6] = 2;
usort($array, "list_cmp");
print_r($array);
?>
OUTPUT:
Array ( [0] => 1 [1] => 1 [2] => 3 [3] => 4 [4] => 2 [5] => 2 [6] => 2 )
QUESTION 7)Write a PHP script to get the shortest/longest string length from an array.
Sample arrays : ("abcd","abc","de","hjjj","g","wer")
ANSWER 7)
INPUT:
<?php
$my_array = array("abcd","abc","de","hjjj","g","wer");
$new_array = array_map('strlen', $my_array);
// Show maximum and minimum string length using max() function and min() function
?>
OUTPUT:
QUESTION 8) Write a PHP script which displays all the numbers between 200 and 250 that are
divisible by 4.
ANSWER 8)
INPUT:
<?php
echo implode(",",range(200,250,4))."\n";
?>
OUTPUT:
200,204,208,212,216,220,224,228,232,236,240,244,248
QUESTION 9)Write a PHP script to calculate and display average temperature, five lowest and
highest temperatures.Recorded temperatures : 78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 75, 76,
73, 68, 62, 73, 72, 65, 74, 62, 62, 65, 64, 68, 73, 75, 79, 73
ANSWER 9)
INPUT:
<?php
$month_temp = "78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73,
68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73";
$tot_temp = 0;
$temp_array_length = count($temp_array);
foreach($temp_array as $temp)
$tot_temp += $temp;
$avg_high_temp = $tot_temp/$temp_array_length;
echo "Average Temperature is : ".$avg_high_temp."
";
sort($temp_array);
?>
OUTPUT:
Average Temperature is : 70.6 List of five lowest temperatures : 60, 62, 63, 63, 64, List of five highest
temperatures : 76, 78, 79, 81, 85,
QUESTION 10) Write a programme in html having a form with Name (firstname+lastname) and
submit button. On clicking button form validation should work (use JavaScript) and data entry should
be saved in database (php code needed for data insertion) form be styled using CSS (by inline,
external or internal mode)
ANSWER 10)
<?php
include("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
</form>
<table border="9">
<tr>
<th>Name</th>
<th>Email</th>
<th>Mobile</th>
<th>Date</th>
</tr>
<?php
while($res=mysqli_fetch_array($result)){
echo '<tr>';
echo '<td>'.$res['name'].'</td>';
echo '<td>'.$res['email'].'</td>';
echo '<td>'.$res['mobile'].'</td>';
echo '<td>'.$res['date'].'</td>';
?>
</table>
</body>
</body>
</html>
In file function.php-
INPUT:
<?php
include("config.php");
if(isset($_POST['submit']))
$name=$_POST['name'];
$email=$_POST['email'];
$mobile=$_POST['mobile'];
$date=$_POST['date'];
if($result)
header("location:insert.php");
else{
echo "failed";
?>
In file config.php-
INPUT:
<?php
$dbhost='localhost';
$dbname='cs tutorial';
$dbusername='root';
$dbpass='';
$mysqli=mysqli_connect($dbhost,$dbusername,$dbpass,$dbname);
?>
SOME OUTPUTS-
1]-Name
Email
Mobile
Date
Submit
2]-