You are on page 1of 23

AMITY UNIVERSITY

Lab/ Practical File


INTRODUCTION TO OPEN SOURCE TECHNOLOGIES
(PHP, MySQL)

Submitted to:

Submitted by:

Ms Bhawana Minocha
BCA + MCA (iv)

Md Adil
A1049514004

INDEX

S.No.

Experiments

1.

Write the PHP script to display all details of PHP server use
phpinfo().

2.

Write the PHP script to show local, global and static scope of a
variable.

3.
4.
5.

Write the PHP script to reverse a given number and calculate its sum.

Write a PHP script to generate a list of first 20 prime numbers


Write a PHP script to calculate factorial of a given number.

6.

Write a PHP script to generate a series of first 20 Fibonacci


number using recursive function.

7.

Write a PHP script to implement atleast seven numeric functions

8.
9.
10.

Write a PHP script to implement atleast seven string functions.


Write a PHP script to compute addition of two matrices.
Write a PHP script to compute multiplication of two matrices.

11.

Write a PHP script to sort the given associative array in


ascending order according to value.

12.

Write a PHP script to sort the given associative array in


descending order according to key

13.

Write a PHP script to show the functionality of date and time


function.

14.

Write a PHP script to implement the functionality of explode()


and implode().

15.

Write a PHP script to implement the functionality of compact()


and extract().

16.
17.
18.

Write a PHP script to search an element using Linear Search.


Write a PHP script to implement form handling using get method.

a PHP script to implement form handling using post method.

Signature/Remarks

1.

Write the PHP script to display all details of PHP server use phpinfo().
<?php
echo

phpinfo();

?>

2. Write the PHP script to show local, global and static scope of a variable.
<?php
$a=4;

//global variable

function variable(){
$b=5; //local variable
print("Local variable");
print nl2br("\n"."Value Of B: ".$b);
print ($a); //cant print out of scope (global variable)
}
function staticVar(){
static $x=10;
//static variable declaration
$x++;

print nl2br("\n\n"."Static Variable"."\nValue Of x: ".$x);


}
variable();
staticVar();
staticVar();
staticVar();
print nl2br("\n\nGlobal variable:\n"."value Of a: ".$a);
?>

3. Write the PHP script to reverse a given number and calculate its sum.
<!DOCTYPE html>
<html>
<head>
<title>Reverse and calculate sum
</title></head>
<body>
<p><h1>Reversing a Number and Calculating Sum</h1>
<form action "result.php" method="post">
Eenter Number: <input type="number" name="num">&nbsp
<input type="submit" name="result" value="Result">
</form>
</br>
</p>
<?php
$num=$_POST['num'];
$num=strrev($num);
echo "Reverse of Integer: ". $num."<br>";
$n=array_sum(str_split($num));
echo "Sum Of Digits: ".$n;
?>
</body>
</html>

4. Write a PHP script to generate a list of first 20 prime numbers


<?php
$num=20;
$number=2; $flag=0;
print nl2br("First 20 Prime Number: \n");
//for($i=1;$i<20;$i++)
while($number>0)
{
for($j=2;$j<=$number;$j++)
{
if ($number%$j==0)
{
break;
}
}
if($number==$j)
{
print nl2br($number."\n");
$flag++;
}
if($flag>=20)
{
break;
}
$number++;
}
?>

5. Write a PHP script to calculate factorial of a given number.


<html>
<body>
<h2>Factorial Calculator</h2>
<form action="fact.php" method="POST">
<input type="text" name="number">
<input type="submit" value="Calculate" > <br><br>
<?php
$j=1;
$number=$_POST["number"];
$a=is_numeric($number);
if($number <0){
echo "--- FACTORIAL OF $NUMBER IS NOT POSSIBLE----<br>";
echo "--Number must be > than equal to 0";
}
elseif($number>=0){
for($i=1;$i<=$number;$i++)
{
$j= $j*$i;
}
}
elseif ($a==False)
echo "";
else{

echo "ada";
}
echo "Factorial of $number is: ", $j;
?>
</body>
</html>

6. Write a PHP script to generate a series of first 20 Fibonacci number using recursive
function
<html>
<body>
<?php
$num = 20;
function fibonacci($num){
if($num==0){
return 0;
}
else if($num==1){
return 1;
}
else {
return (fibonacci($num-1) + fibonacci($num-2));
//recursive statement

}
}
for ($i=0;$i<$num;$i++){
echo fibonacci($i);
echo "&nbsp;&nbsp;&nbsp;";
}
?>
</body>
</html>

7. Write a PHP script to implement atleast seven numeric functions.


<html>
<body>
<h1>Numeric Functions </h1>
<?php
print("Absolute Value: ".abs(-14.6)."<br>"); //abs()- For Absolute
value
print("Ceil() value: ".ceil(12.56)."<br>"); //ceil()-This Round
Up to nearest highest Integer
print("Floor() value: ".floor(12.56)."<br>");//Floor()- This
Rounds to nearest lowsest integer
print("Round() value: ".round(12.50)."<br>"); //Round()-This
Function rounds as floating-point.
print("Max value: ".max(3,4,5,6,7,1,2,9)."<br>"); //max()- finds
maximum of given input
print("Min Value: ".min(3,4,5,6,7,1,2,9)."<br>"); //min()- finds
minimum of given input
print("Power Value: ".pow(2,3)."<br>"); //pow(x,y)- find power of
x to the y
?>
</body>
</html>

8. Write a PHP script to implement atleast seven string functions.


<html>
<body>
<?php
$a="this is my kingdom";
print("<h3>$a</h3>");
print("Length of String: ".strlen($a)."<br>"); //strlen()-finds
length of string
$st="Good Afternoon";
echo str_replace("Afternoon","Morning",$st); //replacing substring
of a string
echo "<br>";
echo strtoupper($a); //change string case to upper case
echo "<br>";
echo strtolower($a); //change string case to lower case
echo "<br>";
echo ucfirst($a); //changes the first character of string to
uppercase
echo "<br>";
echo ucwords($a); //changes case of first character of word of a
string to uppercse
echo "<br>";
echo strpos($a,"y"); //position of last occurence of subsstring in
a string
?>
</body>
</html>

9. Write a PHP script to compute addition of two matrices.

<!doctype html>
<html>
<head>
<style>
.a{
width:50%px;
height:20px;
border-color:blue;
}
table{
display:table;
width:200px;
border-spacing:2px;
}</style>
</head>
<body>
<div align="center">
<h2> Matrix Addition </h2>
<form action="matrix.php" method="post">
<p>Enter Value Of Matrix 1:<br>
<input type="text" name="m1[]" size=13 placeholder="Enter
Element 11">&nbsp
<input type="text" name="m1[]" size=13 placeholder="Enter
Element 12"><br>
<input type="text" name="m1[]" size=13 placeholder="Enter
Element 21">&nbsp
<input type="text" name="m1[]" size=13 placeholder="Enter
Element 22"> </p>
<p>Enter Value Of Matrix 2:<br>
<input type="text" name="m2[]" size=13
Element 11">&nbsp
<input type="text" name="m2[]" size=13
Element 12"><br>
<input type="text" name="m2[]" size=13
Element 21">&nbsp
<input type="text" name="m2[]" size=13
Element 22"> </p>
<input type="submit" name="submit">
</form>
</div>
<?php
foreach( $_POST['m1'] as $values){
$m1[]=$values;
}
//print_r($m1);
echo "<br>";

placeholder="Enter
placeholder="Enter
placeholder="Enter
placeholder="Enter

foreach($_POST['m2'] as $value){
$m2[]=$value;
}
//print_r($m2);
echo "<br>";
for($i=0;$i<sizeof($m1);$i++){
$m3[$i]=$m1[$i]+$m2[$i];
}
echo "<h3 align=center>Result:</h3>";
echo "<table align=center border=2px>";
echo"<tr class=a><td>".$m3[0]."</td><td>".$m3[1]."</td></tr>";
echo"<tr class=a><td>".$m3[2]."</td><td>".$m3[3]."</td></tr>";
echo"</table>";
?>
</body>
</html>

10. Write a PHP script to compute multiplication of two matrices.

<!doctype html>
<html>
<head>
<style>
.a{
width:50%px;
height:20px;
border-color:blue;
}
table{
display:table;
width:200px;
border-spacing:2px;
}</style>
</head>
<body>
<div align="center">
<h2> Matrix Addition </h2>
<form action="matrix_mul.php" method="post">
<p>Enter Value Of Matrix 1:<br>
<input type="text" name="m1[]" size=13 placeholder="Enter
Element 11">&nbsp
<input type="text" name="m1[]" size=13 placeholder="Enter
Element 12"><br>
<input type="text" name="m1[]" size=13 placeholder="Enter
Element 21">&nbsp
<input type="text" name="m1[]" size=13 placeholder="Enter
Element 22"> </p>
<p>Enter Value Of Matrix 2:<br>
<input type="text" name="m2[]" size=13
Element 11">&nbsp
<input type="text" name="m2[]" size=13
Element 12"><br>
<input type="text" name="m2[]" size=13
Element 21">&nbsp
<input type="text" name="m2[]" size=13
Element 22"> </p>
<input type="submit" name="submit">
</form>
</div>
<?php
foreach( $_POST['m1'] as $values){
$m1[]=$values;
}
//print_r($m1);
echo "<br>";

placeholder="Enter
placeholder="Enter
placeholder="Enter
placeholder="Enter

foreach($_POST['m2'] as $value){
$m2[]=$value;
}
//print_r($m2);
echo "<br>";
for($i=0;$i<sizeof($m1);$i++){
$m3[$i]=$m1[$i]*$m2[$i];
}
echo "<h3 align=center>Result:</h3>";
echo "<table align=center border=2px>";
echo"<tr class=a><td>".$m3[0]."</td><td>".$m3[1]."</td></tr>";
echo"<tr class=a><td>".$m3[2]."</td><td>".$m3[3]."</td></tr>";
echo"</table>";
?>
</body>
</html>

11. Write a PHP script to sort the given associative array in ascending order according to
value.

<?php
$age = array("Peter"=>20, "Blue"=>37,
"Knave"=>25,"john"=>45,"whitey"=>65,"Durdon"=>15,"morgan"=>70);
asort($age);
print_r($age);
?>

//sorts array in ascending order based on values

12. Write a PHP script to sort the given associative array in descending order according to
key.

<?php
$age = array("Peter"=>20, "Blue"=>37,
"Knave"=>25,"john"=>45,"whitey"=>65,"Durdon"=>15,"morgan"=>70);
ksort($age);
print_r($age);

//sorts array in ascending order based on keys

?>

13. Write a PHP script to show the functionality of date and time function.
<html>
<body>
<?php
var_dump(checkdate(02,11,2016)); //check whether the given date is
true or false
echo "<br>";
echo "Today's Date is: ".date("d-m-Y")."<br>"; //display today
date.
echo"Today is: ".date("l")."<br>"; //display current day of week
echo"Time is: ".date("h:i:s a")."<br>";
//display current time
am.
$date=date_create("2016-02-12"); //use for creatinng date
$date1=date_create("2016-04-22");
echo "Foramting date: ".date_format($date,"Y/m/d");
echo"<br>"; //use for formating date
date_add($date, date_interval_create_from_date_string('10 days'));
echo date_format($date,"Y/m/d"); echo "<br>"; //adding days in
existing date
$diff=date_diff($date,$date1); //finds difference of dates

echo $diff->format('%R%a days'); echo "<br>";


print_r(getdate()); //for current date
echo "<br>";
print_r(gettimeofday()); echo "<br>"; //for current time of date
print_r(localtime(time(),true));echo "<br>"; //local time
echo strtotime("now") . "<br />"; //to english format
echo strtotime("tomorrow") . "<br />";
$t=time();
//getting current time measured in seconds
echo(date("Y-m-d",$t));
?>
</body>
</html>

14. Write a PHP script to implement the functionality of explode() and implode().
<?php

$st="The Blue Magician";


print "String: ".$st."<br>";
$a=explode(" ",$st);
print("Explode Function: ");
print_r($a); echo"<br><br>";
print("Implode Function: ");
print_r(implode($a," "));
?>

15. Write a PHP script to implement the functionality of compact() and extract().
<?php
$Ben=20;
$x=100;
$c=20;
print("Comapact Function: ");
$a=compact("Ben","x","c");
print_r($a);
echo "<br><br>";
echo "Extract Function: <br>";
$b=array("w"=>10,"y"=>20,"z"=>30);
extract($b);
echo $w."<br>".$y."<br>".$z;
?>

16. Write a PHP script to search an element using Linear Search.


<!DOCTYPE html>
<html>
<head>
<basefont face="Arial">
</head>
<body>
<div style="color:blue;" align="center">
<form action="search.php" method="post">
Search Name:<input type="text" name="search" value="">
</br></br>
<input type="submit" name="submit" value="Search">
</form>
<?php
$a=$_POST['search'];
$name=array("Adil","peter","knave","gulam","blue","marrs","sumin",
"ayuesh","rajat","pallav","ritika");
for($i=0;$i<sizeof($name);$i++){

if($a==$name[$i]){

echo "<script type='text/javascript'>alert('Yo!! Name


Found')</script>";
exit();
}
}
if($a!=$name[$i])
{
echo "<script type='text/javascript'>alert('Sorry!!! Name Not
Found')</script>";
}
?>
</body>
</html>

17.

Write a PHP script to implement form handling using get method.

<html>
<body text-align="center">
<div align="center">
<form action = "<?php $_PHP_SELF ?>" method = "GET">
Name: &nbsp&nbsp<input type = "text" name = "name" /><br>
Course: <input type = "text" name = "course" /> </br><br>
&nbsp&nbsp <input type = "submit" />
</form>
<?php
if( $_GET["name"] || $_GET["course"] ) {
echo "Welcome ". $_GET['name']. "<br />";

echo "You are Enrolled in ". $_GET['course'];


exit();
}
?></div>
</body>
</html>

18. Write a PHP script to implement form handling using post method.
<!DOCTYPE html>
<html>
<head>
<basefont face="Arial">
</head>
<body>
<div style="color:blue;" align="center">
<form action="login.php" method="post">
Username:<input type="text" name="username" value="">
</br>
Password: <input type="password" name="password" value="">
</br></br>
<input type="submit" name="submit" value="Sign Up">
</form>
<?php
$username=$_POST['username'];
$password=$_POST['password'];
if ($username=="adil" && $password=="1234")

{
echo "<script type='text/javascript'>alert('Login
successfull!')</script>";
}
else{
echo "<script type='text/javascript'>alert('Login
failed!')</script>";
}
?>
</div>
</body>
</html>

You might also like