PHP Assignments
1. Write PHP program to find largest of three numbers.
<?php
echo "<h1>PHP Program to print largest of three numbers</h1>";
$n1=100;
$n2=200;
$n3=300;
if($n1>$n2 && $n1>$n3)
{
echo "Largest Number is $n1";
}
if($n2>$n1 && $n2>$n3)
{
echo "Largest Number is $n2";
}
if($n3>$n1 && $n3>$n2)
{
echo "Largest Number is $n3";
}
?>
2. Write PHP program to find factorial of a number.
<?php
echo "<h1>PHP Program to find factorial of a number</h1>";
$f=1;
$i=1;
$n=6;
while($i<=$n)
{
$f=$f*$i;
$i=$i+1;
}
echo "<H1> Factorial=$f</h1>";
?>
3. Write PHP program to find reverse of a number.
<?php
echo "<h1>Program to reverse a number</h1>";
$num=36097;
$rev=0;
$rem;
while($num!=0)
{
$rem=$num%10;
$rev=$rev*10+$rem;
$num=intval($num/10);
}
echo "Reverse of given number is $rev";
?>
4. Write PHP program to check whether a number is prime or not.
<?php
echo "<h1>Program to check a number is prime or not</h1>";
$n=157;
$p=1;
for($i=2;$i<=$n/2;$i++)
{
if($n%$i==0)
{
$p=0;
break;
}
}
if($p==1)
{
echo "Number $n is prime";
}
else
{
echo "Number $n is not prime";
}
?>
5. Write PHP program to add two numbers using function call by value.
<?php
function add($n1,$n2)
{
$result=$n1+$n2;
return $result;
}
echo "Addition result:".add(70,40);
?>
6. Write PHP program to swap two numbers using function call by reference.
<?php
function swap(&$a,&$b)
{
$t=$a;
$a=$b;
$b=$t;
}
$n1=20;
$n2=30;
echo "<br>variable before swap:$n1 and $n2";
swap($n1,$n2);
echo "<br>variable after swap:$n1 and $n2";
?>
7. Write PHP Program to demonstrate use of array function.
<?php
$a=array(10,20,30,40,50);
array_push($a,60);
foreach($a as $e)
{
echo "<br>array element: $e";
}
echo "<br>total elements:".count($a);
?>
8. Write PHP program to find sum of elements of an array.
<?php
echo "<h1>Program to find sum of array elements</h1>";
$a[0]=10;
$a[1]=20;
$a[2]=30;
$a[3]=40;
$a[4]=50;
$s=0;
for($i=0;$i<=4;$i++)
{
$s=$s+$a[$i];
}
echo "<br>Sum of array:".$s;
?>
9. Write PHP program to sort array elements.
<?php
echo "<h1>Program to sort an array</h1>";
$a=array(30,20,10,70,90,50,80,40,100,60);
for($i=0;$i<9;$i++)
{
for($j=$i;$j<10;$j++)
{
if($a[$i]>$a[$j])
{
$t=$a[$i];
$a[$i]=$a[$j];
$a[$j]=$t;
}
}
}
foreach ($a as $e)
{
echo "<br>array element $e";
}
?>
10. Write PHP program to demonstrate use of associative array using for each loop.
<?php
echo "<h1>Program to show associative array</h1>";
$a["roll"]=1;
$a["name"]="Ankit";
$a["address"]="Ranchi";
$a["Graduate"]=true;
foreach ($a as $e)
{
echo "<br>Array Elements:$e";
}
?>
11. Write PHP program to demonstrate use of associative and display contents of array.
<?php
echo "<h1>Program to show associative array</h1>";
$a["roll"]=1;
$a["name"]="Ankit";
$a["address"]="Ranchi";
echo "<br>Students Details";
echo "<br>Roll No:".$a['roll'];
echo "<br>Name:".$a['name'];
echo "<br>Address:".$a['address'];
?>
12. Write PHP Program to find average of three numbers entered by user.
<html>
<head>
<title>Average</title>
</head>
<body bgcolor="pink">
<form method="post" action="average.php">
Number 1<input type="text" name="a"><br>
Number 2<input type="text" name="b"><br>
Number 3<input type="text" name="c"><br>
<input type="submit" value="average">
</form>
<?php
if(isset($_POST['a']))
{
$a=intval($_POST['a']);
$b=intval($_POST['b']);
$c=intval($_POST['c']);
$avg=($a+$b+$c)/3;
echo "average=$avg";
}
?>
</body>
</html>
13. Write PHP Program to find larger of two numbers entered by user.
<html>
<head>
<title>Larger of two</title>
</head>
<body bgcolor="cyan">
<form method="post" action="large.php">
Number 1<input type="text" name="a"><br>
Number 2<input type="text" name="b"><br>
<input type="submit" value="display larger">
</form>
<?php
if(isset($_POST['a']))
{
$a=intval($_POST['a']);
$b=intval($_POST['b']);
if($a>$b)
{
echo"<h1>Larger is =$a</h1>";
}
else
{
echo"<h1>Larger is =$b</h1>";
}
}
?>
</body>
</html>
14. Write a PHP program to demonstrate use of string functions in PHP.
<?php
$str="Sunday";
echo "Today is $str";
echo '<br>Today is $str';
$str="Welcome";
echo "<br>Length of string is :".strlen($str);
echo "<br>Uppercase string :".strtoupper($str);
echo "<br>Lowercase string :".strtolower($str);
echo "<br>Substring :".substr($str,3,4);
echo "<br>Reverse of string :".strrev($str);
echo "<br>String Comparison:".strcmp("hello","hello");
?>
15. Write a PHP program to check whether a string is palindrome or not.
<?php
echo "<h1>Program to check palindrome string</h1>";
$str="malayalam";
$rev=strrev($str);
if(strcmp($str,$rev)==0)
{
echo "String $str is palindrome string";
}
else
{
echo "String $str is not palindrome string";
}
?>
16. Write a PHP program to design login page an check user credentials in server.
<html>
<head>
<title>Login Page</title>
</head>
<body bgcolor="cyan" text="gray">
<h1>User Login</h1>
<form method="post" action="login.php">
User Id<input type="text" name="uid">
<br>Password<input type="password" name="pwd">
<br><Input type="submit" value="login">
<Input type="reset" value="clear">
</form>
<?php
if(isset($_POST["uid"]))
{
$u=$_POST["uid"];
$p=$_POST["pwd"];
if($u=="gossner" && $p=="college")
{
echo "<br>Valid Login Credentials";
?>
<script>
alert("welcome");
window.open("index.php","_self");
</script>
<?php
}
else
{
echo "<br>Invalid Login Credentials";
}
}
?>
</body>
</html>
17. Write a PHP script to convert temperature from Celsius to Fahrenheit.
18. Write a PHP script to print the multiplication table of a given number.
19. Write a PHP script to reverse a given string.
20. Create a PHP script to check whether a given number is even or odd.
21. Write a PHP program that takes two numbers as input using HTML form and performs addition,
subtraction, multiplication, and division. (simple calculator)