You are on page 1of 27

Programming in PHP Lab

FACTORIAL OF A NUMBER
//fact.php
<?php
echo"<b><h1>Factorial of a Number:</b></h1><br>";
$fact=1;
$number=4;
echo"<h3>Factorial of $number:</h3><br>";
for($i=1;$i<=$number;$i++)
{
$fact=$fact*$i;
}
echo $fact;
?>

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

CONDITIONAL STATEMENT
//conditional.html
<html>
<head>
<title> using conditional statement </title>
</head>
<body>
<form method="post" action="conditional.php">
<center><h1><i> student mark statement</i><h1></center><pre>
cloud:<input type=text name=cc><br>
PHP:<input type=text name=php><br>
operating system:<input type=text name=os><br>
<center><input type=submit value=submit></center></pre>
</form>
</body>
</html>

//conditional.php
<?php
$cm=$_POST["cc"];
$phpm=$_POST["php"];
$osm=$_POST["os"];
$mt=$cm+$phpm+$osm;
$avg=$mt/3;
if($cm<30||$phpm<30||$osm<30)
{
echo "The student failed<br>";
}
PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

else
{
echo "The student avg mark is:{$avg}<br>";
switch($avg)
{
case ($avg>"75"):
echo "The student are get:Distiniction";
break;
case ($avg>"60"):
echo "The student are get:First class";
break;
case ($avg>"45"):
echo "The student are get:Second class";
break;
case ($avg>"30"):
echo "the student are get :Third class";
break;
}
}
?>

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

MAXIMUM OF N NUMBERS
//maximum.php
<?php
echo “The Given array is: <br>”;
$arr1[0][]=110;
$arr1[0][]=20;
$arr1[0][]=526;
$arr1[1][]=105;
$arr1[1][]=56;
$arr1[1][]=96;
for($i=0;$i<count($arr1);$i++)
{
for($j=0;$j<count($arr1[$i]);$j++)
{
echo"\$arr[$i][$j]=",$arr1[$i][$j],"<br>";
}}
$b=0;
foreach($arr1 as $val)
{
foreach($val as $key=>$val1)
{
if($val1>$b)
{
$b=$val1;
}
}}
echo "The Largest Number in the array is =" ,$b;
?>

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

GCD of Two Numbers


//gcd.php
<?php
function gcd($a,$b)
{
if($a==0)
{
return $b;
echo $b;
}
elseif($b==0)
{
return $a;
echo $a;
}
elseif($a==$b)
{
return $a;
echo $a;
}
elseif($a>$b)
return gcd($a-$b,$b);
else
return gcd($a,$b-$a);
}
$g=gcd(24,6);
echo'The GCD of $a and $b=',$g;
?>

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

MULTIPLICATION TABLE
//multi.html
<html>
<head>
<title>Multiplication Table</title>
</head>
<form method="post" action="multi.php">
<h1><b>multiplication</b></h1>
<label><b>Enter No:</b></label>
<input type="text"name="num">
<input type="submit"value="send">
<br>
</form>
</body>
</html>

//multi.php
<?php
$n=$_POST['num'];
if($n)
{
for($i=1;$i<=10;$i++)
{
$mul=$n*$i;
echo"$n*$i=$mul<br>";
}
}
?>

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

File Upload
//file1.html
<html>
<head>
<title>file uploads</title>
</head>
<body>
<h1>
File Uploads
</h1>
<form method ="post" action="file1.php"enctype="multipart/form-data">
upload file:<input name="userfile" type="file"/>
<br>
<br>
<input type="submit" value="send file"/>
</form>
</body>
</html>

//file1.php
<?php
$handle = fopen($_FILES['userfile']['tmp_name'],"r");
while (!feof($handle))
{
$text=fgets($handle);
echo $text,"<br>";
}
fclose($handle);
?>
PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

COMPUTING AGE OF GIVEN DATE


//age.html
<html>
<head>
<title>AGE CALCULATOR</title>
</head>
<body>
<form method=post action="age.php">
<h1>AGE CALCULATOR</h1>
Enter Your Date of Birth(eg:yyyy-mm-dd)<input type=text name="dob">
<input type=submit value=submit>
</body>
</html>
//age.php
<?php
function ageCalculator($dob){
if(!empty($dob)){
$birthdate = new DateTime($dob);
$today = new DateTime('today');
$age = $birthdate->diff($today)->y;
return $age;
} else{
return 0;
}}
$dob =$_REQUEST["dob"];
echo "current age is ",ageCalculator($dob);
?>

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

CALCULATOR
//Calculator.php
<html >
<body>
<form method="post">
<h1>Calculator </h1><br><br>
<input type="text" name="n1">
<input type="text" name="n2">
<select name="operator" id="">
<option>None</option>
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<button type="submit" name="submit" value="submit">Calculate</button>
</form>
<?php
if (isset($_POST['submit']))
{
$result1 = $_POST['n1'];
$result2 = $_POST['n2'];
$operator = $_POST['operator'];
switch ($operator) {
case 'None':
echo "You need to select any operator";
break;

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

case 'Add':
echo $result1 + $result2;
break;
case 'Subtract':
echo $result1 - $result2;
break;
case 'Multiply':
echo $result1 * $result2;
break;
case 'Divide':
echo $result1 / $result2;
break;
}
}
?>
</body>
</html>

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

SESSION COUNT
//Session.php
<html>
<head>
<title>session</title>
</head>
<body>
<h1>you have been here</h1>
<?php
session_start();
if(!isset($_SESSION['count']))
{
$_SESSION['count']=0;
}
else
{
$_SESSION['count']++;
}
echo $_SESSION['count'];
?>
<h1>times before</h1>
</body>
</html>

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

DRAWING HUMAN FACE

//img1.php
<?php
$image_height=100;
$image_width=300;
$image=imagecreate($image_width,$image_height);
$backcolor=imagecolorallocate($image,200,200,200);
$drawing_color=imagecolorallocate($image,0,0,0);
imagearc($image,150,50,50,50,30,150,$drawing_color);
imagearc($image,150,50,70,70,0,360,$drawing_color);
imagearc($image,135,45,20,20,190,-10,$drawing_color);
imagearc($image,165,45,20,20,190,-10,$drawing_color);
imagearc($image,135,42,10,10,-10,190,$drawing_color);
imagearc($image,165,42,10,10,-10,190,$drawing_color);
header("content-type:image/jpeg");
imagejpeg($image);
imagedestroy($image);
?>

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

COOKIE

//cook.php
<html>
<head>
</head>
<body>
<center>
<h2>last visited time on the webpage</h2>
</center><br>
<?php
$inTwoMonths=60*60*24*60+time();
setcookie('lastvisit',date("G:i-d/m/y"),$inTwoMonths);
if(isset($_COOKIE['lastvisit']))
{
$visit=$_COOKIE['lastvisit'];
echo"your last visit was-",$visit;
}
else
echo"you have got some stale cookies";
?>
</body>
</html>

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

USER AUTHENTICATION
//login.html
<html>
<body>
<form method=get action="login.php">
<h1>USER AUTHENTICATION</h1>
USER ID<input type=text name="userid"><br><br>
PASSWORD<input type=password name="password">
<input type=submit value=submit>
</body>
</html>

//login.php
<?php
$t1=$_REQUEST["userid"];
$t2=$_REQUEST["password"];
$connect=mysql("localhost","@");
@mysql_select_db("test");
$query="SELECT * FROM users";
$result=mysql_query($query);
$num=mysql_num_rows($result);
$i=0;
while($i<$num)
{
$n1=mysql_result($result,$i,"userid");
$n2=mysql_result($result,$i,"password");
if($t1=="$n1" and $t2=="$n2")
{
$flag=true;

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

}
else
{
$flag=false;
}
$i++;
}
if($flag)
echo "Authenticated User";
else
echo "Username or Password Incorrect";
?>

Users Table:

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi
Programming in PHP Lab

PG & Research Department of Computer Science, STET Women’s College, Sundarakkottai, Mannargudi

You might also like