Beruflich Dokumente
Kultur Dokumente
No: ..............
Aim
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>
Result
The above program has been successfully executed and the output is obtained.
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>
Result
The above program has been successfully executed and the output is obtained.
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;
}
}
?>
<body>
</html>
Result
The above program has been successfully executed and the output is obtained.
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>
?>
</fieldset>
<body>
</html>
Result
The above program has been successfully executed and the output is obtained.
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>
Result
The above program has been successfully executed and the output is obtained.
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>
Result
The above program has been successfully executed and the output is obtained.
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;
}
}
Result
The above program has been successfully executed and the output is obtained.
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>";
}
?>
Result
The above program has been successfully executed and the output is obtained.
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>";
}
Result
The above program has been successfully executed and the output is obtained.