Sie sind auf Seite 1von 26

Department of Expt

No: ..............
Aim

Create an associative array with book details and display it in a table.

Source Code

<html>
<head>
<title>associate array</title>
</head>
<body>
<form method='POST'>
enter the course:
<select name="1">
<option value="cs">CS
<option value="biology">Biology
<option value="commerse">Commerce
</select>
<input type="submit" value="draw table">
</form>
<?php
if($_POST)
{
$arr=$_POST['1'];
$book=array('cs'=>array('java'=>200,'dbms'=>200),'biology'=>array('neuo'=>100,'gen'=>300
),'commerse'=>array('stati'=>200,'tally'=>410));
echo "<table border=4>";
foreach($book as $key=>$value)
{
foreach($value as $key2=>$value2)
{
if($arr==$key)
{
echo "<tr>";
echo "<td>".$key2."</td><td>".$value2."</td>";
echo "</tr>";
}
}
}
echo "</table>";
}
?>
</body>

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
</html>

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
Output

Result

The above program has been successfully executed and the output is obtained.

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
Aim

Write a program to create an array and try with all array functions.

Source Code

<html>
<head>
<title> array funtion</title>
</head>
<body>
<?php
$arr = array('one', 'two', 'three', 'four');
$str = implode(' and ', $arr);
echo " implode ";
echo "<br>";
print_r($arr);
echo $str;
echo "<br>";
$arr1 = 'one,two,three,four';
echo " explode ";
echo "<br>";
$str1 = explode(',', $arr1);
print_r($str1);
echo "<br>";
echo 'Minimum is ' . min($arr) . ' and maximum is ' . max($arr);
echo "<br>";
echo "result after slice ";
echo "<br>";
$arr2 = array_slice($arr, 1, 2);
print_r($arr2);
echo "<br>";
array_shift($arr);
echo "result after shift ";
echo "<br>";
print_r($arr);
echo "<br>";
array_push($arr, 'one');
echo "result after push ";
echo "<br>";
print_r($arr);
?>
</body>

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
</html>

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
Output

Result

The above program has been successfully executed and the output is obtained.

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
Aim

Make a page that would accept your 5 favorite names. Save it in an array. Then, identify
what is the shortest name entered and the index where it is located in an array.

Source Code

<html>
<head>
<title> Shortest Name</title>
</head>
<body>
<form method='POST'>
<input type="text" name="1">
<input type="submit" value="CREATE">
</form>
<?php
if($_SERVER['REQUEST_METHOD']=="POST")
{
$ch = $_POST['1'];
$arr=explode(',',$ch);
$len=sizeof($arr);
$min = 100;
for($i=0;$i<$len;$i++)
{
$a=$arr[$i];
$length = strlen($a);
echo $a." : ".$length."<br>";
if($length < $min)
{
$min = $length;
$ind = $i;
}
}

echo "<br><br><b>SHORTEST NAME : <b>".$arr[$ind]."<br><b> INDEX :<b>".$ind;


}

?>
<body>
</html>

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
Output

Result

The above program has been successfully executed and the output is obtained.

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
Aim

Make a page that would accept 5 favorite subjects and the marks &grades you got on
each subject. Save it in a multidimensional array. Then compute the average of the 5
subjects.

Source Code

<html>
<head>
<title> Shortest Name</title>
</head>
<body>
<form method='POST'>
<fieldset>
<legend>Student data</legend>
<label>Subject 1: </label>
<input type="text" name="1">
<label>Mark: </label>
<input type="text" name="2">
<label>Grade: </label>
<input type="text" name="3"><br>
<label>Subject 2: </label>
<input type="text" name="4">
<label>Mark: </label>
<input type="text" name="5">
<label>Grade: </label>
<input type="text" name="6"><br>
<label>Subject 3: </label>
<input type="text" name="7">
<label>Mark: </label>
<input type="text" name="8">
<label>Grade: </label>
<input type="text" name="9"><br>
<label>Subject 4: </label>
<input type="text" name="10">
<label>Mark: </label>
<input type="text" name="11">
<label>Grade: </label>
<input type="text" name="12"><br>
<label>Subject 5: </label>
<input type="text" name="13">
<label>Mark: </label>

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
<input type="text" name="14">
<label>Grade: </label>
<input type="text" name="15"><br>
<input type="submit" value="FIND">
</form>
<?php
if($_SERVER['REQUEST_METHOD']=="POST")
{
$arr[0][0] = $_POST['1'];
$arr[0][1]= $_POST['2'];
$arr[0][2] = $_POST['3'];
$arr[1][0] = $_POST['4'];
$arr[1][1]= $_POST['5'];
$arr[1][2] = $_POST['6'];
$arr[2][0] = $_POST['7'];
$arr[2][1] = $_POST['8'];
$arr[2][2] = $_POST['9'];
$arr[3][0]= $_POST['10'];
$arr[3][1] = $_POST['11'];
$arr[3][2] = $_POST['12'];
$arr[4][0] = $_POST['13'];
$arr[4][1]= $_POST['14'];
$arr[4][2]= $_POST['15'];
$avg=($arr[0][1]+$arr[1][1]+$arr[2][1]+$arr[3][1]+$arr[4][1])/5;
echo "<br><br><b>AVERAGE MARK : <b>".$avg."<br>";
}

?>
</fieldset>
<body>
</html>

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
Output

Result

The above program has been successfully executed and the output is obtained.

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
Aim

Write a program that reads an array of numbers and returns a list of all those numbers
less than 15.

Source Code

<html>
<head>
<title> Shortest Name</title>
</head>
<body>
<form method='POST'>
<fieldset>
<legend>Enter Numbers</legend>
<input type="text" name="1">
<input type="submit" value="Check">
</form>
<?php
if($_SERVER['REQUEST_METHOD']=="POST")
{
$ch = $_POST['1'];
$arr=explode(',',$ch);
$len=sizeof($arr);
for($i=0;$i<$len;$i++)
{
$a=$arr[$i];
if($a < 15)
{
echo "<br><br><b>$a IS LESS THAN 15 <b>";
}
}
}
?>
</fieldset>
<body>
</html>

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
Output

Result

The above program has been successfully executed and the output is obtained.

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
Aim

Write a program that reads an array and returns a message indicating whether the array
contains only unique values.

Source Code

<html>
<head>
<title> Shortest Name</title>
</head>
<body>
<form method='POST'>
<fieldset>
<legend>Enter Numbers</legend>
<input type="text" name="1">
<input type="submit" value="Check">
</form>
<?php
if($_SERVER['REQUEST_METHOD']=="POST")
{
$ch = $_POST['1'];
$arr=explode(',',$ch);
$len=sizeof($arr);
$arr1=array_unique($arr);
echo "<br>";
print_r($arr);
echo "<br>";
print_r($arr1);
$len1=sizeof($arr1);
echo "<br>";
if($len==$len1)
{
echo "array elements are unique";
}
else
{
echo "array elements are not unique";
}
}
?>
</fieldset>
<body>

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
</html>

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
Output

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............

Result

The above program has been successfully executed and the output is obtained.

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
Aim

Create a webpage to accept two numbers and find out the LCM and GCF using
functions.

Source Code

<html>
<head>
<title>lcm and gcf of numbers</title>
</head>
<body>
<form method="POST">
enter the first number :
<input name="1st" type="text"></input><br>
enter the second number :
<input name="2st" type="text"></input><br>
<input type="submit" value="submit" name="submit"></input>
</form>
<?php
if(isset($_POST['submit']))
{
$num1 = $_POST ['1st'];
$num2 = $_POST ['2st'];
$t1 = $_POST ['1st'];
$t2 = $_POST ['2st'];

function lcm($num1,$num2)
{
$max=($num1>$num2) ? $num1 : $num2;
while(1)
{
if($max%$num1==0 && $max%$num2==0)
{
echo "LCM of " .$num1. " and " .$num2. " is: ".$max;
break;
}
$max=$max+1;
}
}

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
function gcd($num1,$num2)
{
while($num1!=$num2)
{
if($num1 > $num2)
$num1=$num1-$num2;
else $num2=$num2-$num1;
}
return $num2;
}
lcm($num1,$num2);
echo "<br>";
$a=gcd($num1,$num2);
echo "gcf of " .$t1. " and " .$t2. " is: ".$a;
}
?>
</body>
</html>

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
Output

Result

The above program has been successfully executed and the output is obtained.

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
Aim

Write a program to check whether the given number is prime or not using functions.

Source Code

<html>
<head>
<title>lcm and gcf of numbers</title>
</head>
<body>
<form method="POST">
enter the first number :
<input name="1st" type="text"></input><br>
<input type="submit" value="submit" name="submit"></input>
</form>
<?php
if(isset($_POST['submit']))
{
$num1 = $_POST ['1st'];
function IsPrime($n)
{
for($x=2; $x<$n; $x++)
{
if($n %$x ==0)
{
return 0;
}
}
return 1;
}
$a = IsPrime($num1);
if ($a==0)
echo "This is not a Prime Number....."."<br>";
else
echo "This is a Prime Number.."."<br>";
}
?>

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
Output

Result

The above program has been successfully executed and the output is obtained.

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
Aim

Display the Fibonacci series for particular limit.(Use for loop) Create a string of names
consisting of "John, Jerry, Ann, Sanju, Wen, Paul, Louise, Peter".
a. Create a single dimensional array out of the string.
b. Get the total number of values in the array
c. Search for a specific value in the array.
d. to sort the array by value in ascending order
e. to sort the array by value in reverse order.
f. Remove the first element from the array.
g. Add "Willie" and "Daniel" to the end of the array.
h. Replace "Paul" with "Andre".
i. Add "Alisha" to the beginning of the array.
j. Output the minimum and maximum value of the array
k. Randomly arrange the values of the array
l. Create another array of names. Choose names of your choice.
m. Merge both of your arrays together and display them in sorted order.

Source Code

<html>
<head>
<title> Associative Array</title>
</head>
<body>
<form method='POST'>
<br><br><input type="text" name="1">
<input type="submit" value="Search">
<br><br><input type="text" name="2">
<input type="submit" value="Create array">
</form>
<?php
if($_SERVER['REQUEST_METHOD']=="POST")
{
$names='John,Jerry,Ann,Sanju,Wen,Paul,Louise,Peter';
$ch = $_POST['1'];
$arr=explode(',',$names);
$len=sizeof($arr);
echo "<br><br><b>The names are:</b><br>";
for($i=0;$i<$len;$i++)
{
echo $arr[$i]."<br>";
}

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
print_r($arr);
echo "<br><br><b>Total number of values : <b>".$len."<br></b>";
$key=array_search($ch,$arr);
if($key!=NULL)
echo "<br>".$ch. " is in postion ".$key."<br>";
else
echo"<br>".$ch." not found<br>";
echo "<br> Array after ascending sort <br></b>";
sort($arr);
print_r($arr);
echo "<b><br><br> Array after reverse sort <br></b>";
rsort($arr);
print_r($arr);
echo "<b><br><br> Array after removing first element <br></b>";
$ar=array_shift($arr);
print_r($arr);
echo "<b><br><br> Array after adding two elements <br></b>";
array_push($arr,"Willie","Daniel");
print_r($arr);
echo "<b><br><br> Array after replacing element <br></b>";
@$nkey=$key = array_search("Paul", $arr);
$arr[$nkey] = "Andre";
print_r($arr);
echo "<b><br><br> Array after adding one element to beginning <br></b>";
array_unshift($arr,"Alisha");
print_r($arr);
echo "<b><br>Minimum value : ".min($arr)." Maximum value: ". max($arr)."</b>";
echo "<b><br><br> Array after Shuffling <br></b>";
shuffle($arr);
print_r($arr);
echo "<b><br><br> New array <br></b>";
@$newstr=$_POST['2'];
@$name=explode(',',$newstr);
print_r($name);
echo "<b><br><br> Array after merging <br></b>";
print_r(array_merge($arr,$name));
echo "<b><br><br> Array after sorting <br></b>";
$arr=array_merge($arr,$name);
sort($arr);
print_r($arr);
}
?>
<body>
</html>

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............

Federal Institute of Science and


Technology (FISAT) TM
Department of Expt
No: ..............
Output

Result

The above program has been successfully executed and the output is obtained.

Federal Institute of Science and


Technology (FISAT) TM

Das könnte Ihnen auch gefallen