PHP Lab File
PHP Lab File
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.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
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++;
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"> 
<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>
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 " ";
}
?>
</body>
</html>
<!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"> 
<input type="text" name="m1[]" size=13 placeholder="Enter
Element 12"><br>
<input type="text" name="m1[]" size=13 placeholder="Enter
Element 21"> 
<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"> 
<input type="text" name="m2[]" size=13
Element 12"><br>
<input type="text" name="m2[]" size=13
Element 21"> 
<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>
<!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"> 
<input type="text" name="m1[]" size=13 placeholder="Enter
Element 12"><br>
<input type="text" name="m1[]" size=13 placeholder="Enter
Element 21"> 
<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"> 
<input type="text" name="m2[]" size=13
Element 12"><br>
<input type="text" name="m2[]" size=13
Element 21"> 
<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);
?>
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);
?>
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
14. Write a PHP script to implement the functionality of explode() and implode().
<?php
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;
?>
if($a==$name[$i]){
17.
<html>
<body text-align="center">
<div align="center">
<form action = "<?php $_PHP_SELF ?>" method = "GET">
Name:   <input type = "text" name = "name" /><br>
Course: <input type = "text" name = "course" /> </br><br>
   <input type = "submit" />
</form>
<?php
if( $_GET["name"] || $_GET["course"] ) {
echo "Welcome ". $_GET['name']. "<br />";
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>