PHP Assignments
PHP Assignments
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);
?>
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>
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>
18. Write a PHP script to print the multiplication table of a given number.
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)