Php Program1to8
Php Program1to8
PROGRAMS
1. Create a PHP program to display the bio data of the person by reading the personal details using
an HTML page.
Program code
<!DOCTYPE HTML>
<html>
<head>
<title>Biodata</title>
</head>
<body><fieldset><legend>BIODATA</legend>
<form method="POST">
<table>
<tr><td> Name </td><td>: <input type="text" name="txt_name"/></td></tr>
<tr><td> Date of birth</td><td>: <input type="text" name="txt_date"/></td></tr>
<tr><td> Sex </td><td>: <select name="sex">
<option value="">Select</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</td></tr>
<tr><td> Religion</td><td>: <input type="text" name="txt_rg"/></td></tr>
<tr><td> Cast </td><td>: <input type="text" name="txt_ct"/></td></tr>
<tr><td> Nationality </td><td>: <input type="text" name="txt_nt"/></td></tr>
<tr><td> Father's name </td><td>: <input type="text" name="txt_fn"/></td></tr>
<tr><td> Mother's name </td><td>: <input type="text" name="txt_mn"/></td></tr>
<tr><td> Address</td><td>: <textarea name="txt_adr"></textarea></td></tr>
<tr><td></td><td><input type="submit" value="Show" name="btn_show"/></td></tr></table>
<?php
if(isset($_POST['btn_show']))
{
$name=$_POST['txt_name'];
$db=$_POST['txt_date'];
$sex=$_POST['sex'];
$rg=$_POST['txt_rg'];
$ct=$_POST['txt_ct'];
$nt=$_POST['txt_nt'];
$fn=$_POST['txt_fn'];
$mn=$_POST['txt_mn'];
$adr=$_POST['txt_adr'];
echo "BIODATA<br>";
echo"********<br>";
echo"Name:$name<br>";
echo"Date of birth:$db<br>";
echo"Sex:$sex<br>";
echo"Religion:$rg<br>";
echo"Cast:$ct<br>";
echo"Nationality:$nt<br>";
echo"Father's name:$fn<br>";
echo"mother's name:$mn<br>";
echo"Address:$adr<br>";
}
?>
</fieldset>
</form>
</body>
</html>
Program code
<!DOCTYPE HTML>
<html>
<head>
<title>Biodata</title>
</head>
<body><fieldset><legend>Reverse a string</legend>
<form method="POST">
<table>
<tr><td> Enter a string </td><td>: <input type="text" name="txt_str"/></td></tr>
<tr><td></td><td><input type="submit" value="Reverse" name="btn_rv"/></td></tr></table>
<?php
if(isset($_POST['btn_rv']))
{
$str=$_POST['txt_str'];
echo"Reversed strin is:",strrev($str);
}
?>
</fieldset>
</form>
</body>
</html>
3. Write a PHP program to check whether the given number is perfect, abundant or deficient.
Program code
<!DOCTYPE HTML>
<html>
<head>
</head>
<body><fieldset><legend>Perfect or Deficient or Abundant number</legend>
<form method="POST">
<table>
<tr><td> Enter a number </td><td>: <input type="text" name="txt_num"/></td></tr>
<tr><td></td><td><input type="submit" value="Submit" name="btn"/></td></tr></table>
<?php
if(isset($_POST['btn']))
{
$n=$_POST['txt_num'];
$sum=0;
for($i=1;$i<$n;$i++)
{
if($n%$i==0)
$sum=$sum+$i;
}
if($sum==$n)
echo"$n is Perfect
number"; else
if($sum<$n)
echo"$n is Deficient
number"; else
echo"$n is Abundant number";
}
?>
</fieldset>
</form>
</body>
</html>
4. Write a PHP script to upload the file to the server through HTML
Page
upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload</title>
</head>
<body>
<h2>Upload a File</h2>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Choose file to upload:</label>
<input type="file" id="file" name="file" required>
<br><br>
<input type="submit" value="Upload File">
</form>
</body>
</ht
ml>
upload.php
<?php
$target_dir = "uploads/";
5. PHP program to store current date-time in a cookie and display the last visited date-time on the
web page upon revisiting the same web pages
Program code
<!DOCTYPE HTML>
<html>
<head>
<title>cookies</title>
</head>
<body>
<?php
$cookie_expiration_time = time() + ( 60 * 60 * 24 * 60);
$cookie_name = "last_visit";
if (isset($_COOKIE[$cookie_name])) {
$last_visit = $_COOKIE[$cookie_name];
echo "Your last visit was on: " . $last_visit . "<br>";
} else {
echo "This is your first visit!<br>";
}
6. Write an HTML page to display a list of fruit in a list box. Write a PHP program to display
the selected fruit in a webpage.
Program code
<!DOCTYPE HTML>
<html>
<head>
<title>Fruit list</title>
</head>
<body><fieldset><legend>Fruit list</legend>
<form method="POST">
<table>
<tr><td> Select fruit name</td><td>: <select name="fruit">
<option value="">Select</option>
<option value="Mango">Mango</option>
<option value="Banana">Banana</option>
<option value="Grape">Grape</option>
<option value="Orange">Orange</option>
</td></tr>
<tr><td></td><td><input type="submit" value="Show" name="btn_show"/></td></tr>
</table>
<?php
if(isset($_POST['btn_show']))
{
$fruit=$_POST['fruit'];
echo"Selecter fruit is:$fruit";
}
?>
</fieldset>
</form>
</body>
</html>
7. Write a PHP program to create an array and store 10 names in the array. Do the following
operations.
a. Display the content using for each statement.
b. Display the array in a sorted order.
c. Display the array without the duplicate elements.
d. Remove the last element and display.
e. Display the array in reverse order.
f. Search an element in the given array.
Program code
<!DOCTYPE HTML>
<html>
<head>
<title>Array operations</title>
</head>
<body><fieldset><legend>Array operations</legend>
<form method="POST">
<table>
<tr><td>Display array elements</td><td><input type="submit" value="Submit"
name="btn_disp"/></td></tr>
<tr><td>Sort array elements</td><td><input type="submit" value="Submit"
name="btn_sort"/></td></tr>
<tr><td>Remove duplicate elements</td><td><input type="submit" value="Submit"
name="btn_dup"/></td></tr>
<tr><td>Remove last element</td><td><input type="submit" value="Submit"
name="btn_rm"/></td></tr>
<tr><td>Reverse array elements</td><td><input type="submit" value="Submit"
name="btn_rvs"/></td></tr>
<tr><td>Search an array elements</td><td>: <input type="text"
name="txt_name"/></td><td><input type="submit" value="Submit" name="btn_srch"/></td></tr>
</table>
</fieldset>
</form>
</body>
</html>
<?php
$a=array("Anu","Anju","Arjun","Manju","Meera","Anu","Vinu","Adhi","Anu","Neena");
if(isset($_POST['btn_disp']))
{
echo "Array elements are... <br>";
foreach($a as $n)
echo "$n <br>";
}
if(isset($_POST['btn_sort']))
{
sort($a);
echo "Sorted array elements are... <br>";
foreach($a as $n)
echo "$n <br>";
}
if(isset($_POST['btn_dup']))
{
$s=array_unique($a);
echo "Array without duplicate elements are... <br>"; foreach($s
as $n)
echo "$n <br>";
}
if(isset($_POST['btn_rm']))
{
array_pop($a);
echo "Removing last elements .. <br>";
foreach($a as $n)
echo "$n <br>";
}
if(isset($_POST['btn_rvs']))
{
$s=array_reverse($a);
echo "Array in reverse order... <br>";
foreach($s as $n)
echo "$n <br>";
}
if(isset($_POST['btn_srch']))
{
$s=$_POST['txt_name'];
if(in_array($s,$a))
echo "Element present in the array <br>";
else
echo "Element not present";
}
?>
Multiplication.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multiplication Table</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h2>Multiplication Table Generator</h2>
<form id="multiplicationForm">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required>
<button type="submit">Generate Table</button>
</form>
<div id="result"></div>
<script>
$(document).ready(function () {
$("#multiplicationForm").on("submit", function (e) {
e.preventDefault(); // Prevent form submission
generate_table.php
<?php
if($_POST)
{
$num = $_POST["number"];