Php lab

 EX.NO: 1

DATE :

NAVIGATION

AIM:

To write a program to design a web page with links to different pages and allow navigation

between web pages.

PROGRAM:

<html>

<head>

<title>Navigation bar</title>

</head>

<body>

<h1>PHP PROGRAM</h1>

<?php

if(isset($_GET['page'])){

$page=$_GET['page'];

if($page==='factorial'){

include('factorial.php');}

elseif($page==='prime'){

include('prime.php');}

if($page==='valid mail'){

include('valid mail.php');}}

?>

<hr>

<h3>NAVIGATION</h3>

<ul><li><a href="factorial.php?page=home">FACTORIAL</a></li>

<li><a href="prime.php?about">PRIME NUMBER</a></li>

<li><a href="valid mail.php?page=contact">VALID MAIL<a></li></ul>

</body>

</html>

.........

EX.NO: 2

DATE :

REGISTRATION

AIM:

To Write a program to design a webpage with a form that uses all types of controls.

PROGRAM:

<html>

<body>

<?php

$nameErr=$genderErr=$websiteErr="";

$name=$gender=$comment=$website="";

if($_SERVER["REQUEST_METHOD"]=="POST"){

if(empty($_POST["name"])){

$nameErr="Name is required";}

else{

$name=test_input($_POST["name"]);

if(!preg_match("/^['a-zA-Z-']*$/",$name)){

$nameErr="only letters and white space allowed";}}

if(empty($_POST["comment"])){

$comment="";}

else{

$comment=test_input($_POST["comment"]);}

if(empty($_POST["gender"])){

$genderErr="Gender is required";}

else{

$gender=test_input($_POST["gender"]);}}

functiontest_input($data){

$data=trim($data);

$data=stripslashes($data);

$data=htmlspecialchars($data);

return $data;}

?>

<h2>PHP form validation example</h2>

<p><span class="error">*required field</span></p>

<form method="post" action="<?php echo

htmlspecialchars($_SERVER["PHP_SELF"]);?>">

Name:<input type="text" name="name" value="<?php echo $name;?>">

<span class="error">*<?php echo $nameErr;?></span>

<br><br>

Comment:<textarea name="comment"rows="5"cols="40"><?php echo

$comment;?></textarea>

<br><br>

Gender:

<input type="radio"name="gender"<?php if(isset ($gender)&&$gender=="female")echo

"checked";?>value="Female">Female

<input type="radio"name="gender"<?php if(isset ($gender)&&$gender=="male")echo

"checked";?>value="male">Male

<input type="radio"name="gender"<?php if(isset ($gender)&&$gender=="other")echo

"checked";?>value="other">other

<span class="error">*<?php echo $genderErr;?></span>

<br><br>

<input type="submit"name="submit"value="submit"></form>

<?php

echo "<h2>your input:</h2>";

echo $name;

echo "<br>";

echo $comment;

echo "<br>";

echo $gender;

?>

</body>

</html>

.........


EX.NO: 3

DATE :

BIGGEST OF THREE NUMBERS

AIM:

To write program to create a page using functions for comparing three integers and print the

largest number.

PROGRAM:

<html>

<head>

<meta http-equiv="Content-Type"content="text/html;charset=UTF-8">

<title>

PHP program to find largest of three numbers

</title>

</head>

<body>

<h3>PHP program to find largest of three numbers</h3>

<form action=""method="post">

Number1:

<input

type="number"name="first"min="0"value="<?=(isset($_POST['first']))?$_POST['first']:25;?>

"/>

<br/><br/>

Number2:<input

type="number"name="second"min="0"value="<?=(isset($_POST['second']))?$_POST['secon

d']:75;?>"/><br/><br/>

Number3:<input

type="number"name="third"min="0"value="<?=(isset($_POST['third']))?$_POST['third']:53;

?>"/><br/><br/>

<input type="submit"><br/><br/>

</form>

</body>

</html>

<?php

if($_POST){

$first=$_POST['first'];

$second=$_POST['second'];

$third=$_POST['third'];

$temp=($first>$second)?$first:$second;

$largest=($third>$temp)?$third:$temp;

print"<h2>largest of three number is:".$largest."</h2>";}

?>

........

EX.NO:4

DATE :

FACTORIAL

AIM:

To write a function to calculate the factorial of a number (non-negative integer).The function

accept the number as an argument.

PROGRAM:

<html>

<head>

<title>Factorial Number</title>

</head>

<body>

<form method="post">

<table border="0">

<tr>

<td><input type="text"name="num"value=""placeholder="Enter a Number"/></td></tr>

<tr><td><input type="submit"name="submit"value="submit"/></td>

</tr>

</table></form>

<?php

if(isset($_POST['submit'])){

$n=$_POST['num'];

$f=1;

for($i=1;$i<=$n;$i++){

$f=$f*$i;}

echo"Factorial Of Number".$n."is:".$f;

return 0;}

?>

</body>

</html>

.........

EX.NO: 5

DATE :

NUMBER TO WORD CONVERTER

AIM:

To write a program to covert number into word.

PROGRAM:

<html>

<head>

<title>NUMBER TO WORD CONVERTER</title>

</head>

<body>

<h1>number to word converter</h1>

<form method="POST">

<input type="number"name="n"placeholder="ENTER A NUMBER"/>

<button type="submit">convert</button>

</form>

<?php

if($_SERVER["REQUEST_METHOD"]=="POST"){

$n=$_POST["n"];

functionconvertnumbrttoword($n){

$ones=array(

0=>'zero',

1=>'one',

2=>'two',

3=>'three',

4=>'four',

5=>'five',

6=>'six',

7=>'seven',

8=>'eight',

9=>'nine'

);

$teens=array(

10=>'ten',

11=>'eleven',

12=>'twelve',

13=>'thirteen',

14=>'fourteen',

15=>'fifteen',

16=>'sixteen',

17=>'seventeen',

18=>'eighteen',

19=>'nineteen'

);

$tens=array(

2=>'twenty',

3=>'thirty',

4=>'forty',

5=>'fifty',

6=>'sixty',

7=>'seventy',

8=>'eighty',

9=>'ninety',

);

if($n<10){

return $ones[$n];}

elseif($n<20){

return $teens[$n];}

elseif($n<100){

$tensdigit=floor($n/10);

$onesdigit=$n%10;

$result=$tens[$tensdigit];

if($onesdigit>0){

$result.=".$ones[$onesdigit];}

return result;}

elseif($n<1000){

$hundredsdigit=floor($n/100);

$remainder=$n%100;

$result=$ones[$hundredsdigit].'hundred';

if($remainder>0){

$result.=".convertnumbertoword[$remainder];}

return $result;}

else{

return 'number out of range';}}

echo'<p>'.convertnumbrttoword($n).'</p>';}

?>

</body>

</html>

...........

EX.NO: 6

DATE :

PRIME OR NOT

AIM:

ToWrite a program to check whether the given number is prime or not.

PROGRAM:

<html><head>

<title>PHP PROGRAM TO CHECK A GIVEN NUMBER IS PRIME OR NOT</title>

</head><body>

<form method="POST">

<table border="0"><tr>

<td><input type="text"name="num"value=""placeholder="enter positive

integer"/></td></tr><tr>

<td><input type="submit"name="submit"value="submit"/></td></tr>

</table></form>

<?php

if(isset($_POST['submit'])){

$n=$_POST['num'];

$flag=0;

for($i=2;$i<=$n/2;$i++){

if($n%$i==0){

$flag=1;

break;}}

if($flag==0)

echo"$n is a prime number";

else

echo"$n is not a prime number";

return 0;}

?>

.........

</body></html>

EX.NO: 7

DATE :

PALINDROME OR NOT

AIM:

To Write a program that checks whether a passed string is palindrome or not.

PROGRAM:

<html><head>

<title>PALINDROME CHECKER</title>

</head><body>

<h1>Palindrome Checker</h1>

<form method="POST">

<input type="text"name="inputstring"placeholder="Enter a string"/>

<button type="submit">CHECK</button></form>

<?php

if($_SERVER["REQUEST_METHOD"]=="POST"){

$inputstring=$_POST["inputstring"];

functionispalindrome($string){

$string=strtolower(str_replace("","",$string));

$reversedString=strrev($string);

if($string==$reversedString){

return true;}

else{

return false;}}

if(ispalindrome($inputstring)){

echo"<p>the string '$inputstring' is a palindrome.</p>";}

else{

echo"<p>the string '$inputstring' is not a palindrome.</p>";}}

?>

........

</body></html>

EX.NO: 8

DATE :

GREETING MESSAGE

AIM:

To write a program to create a PHP page which accepts name from user to display good

morning or good evening message along with user name based on time function.

PROGRAM:

<html>

<head>

<title>GREETING MESSAGE</title>

</head>

<body>

<h3>Displaying Greeting Depending Upon Current Time Zone</h3>

<?php

date_default_timezone_set("asia/kolkata");

$h=date('G');

if($h>=5&&$h<=11){

echo"GOOD MORNING";}

elseif($h>12&&$h<=15){

echo"GOOD AFTERNOON";}

else{

echo"GOOD EVENING";}

?>

</body>

</html>

......

EX.NO: 9

DATE :

BIRTHDAY COUNTDOWN

AIM:

 To write a program to create a simple birthday countdown script the script will count the number

of days between current day and birth day.

PROGRAM:

<html><head><?php

$birthday="2024-7-29";

$currentdate=date("y-m-d");

$diff=strtotime($birthday)-strtotime($currentdate);

$days=floor($diff/(60*60*24));

$minutes=floor($days*1440);

$hours=floor(($minutes/60)/(60*60));

$seconds=$minutes*60;

echo"countdown to birthday:";

echo$days."days,";

echo$hours."hours,";

echo$minutes."minutes,";

echo$seconds."seconds";

?></head></html>

.......

EX.NO:10

DATE :

VALID MAIL OR NOT

AIM:

To Write a program to check the email id is valid or not using regular expression.

PROGRAM:

<html>

<head>

<title>vaild mail or not</title>

</head>

<body>

<form method="post">

<label for="name">enter your mail id:

</label>

<input type="text"id="email"name="email">

<input type="submit"value="submit"></form>

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){

$email=$_POST['email'];

if(filter_var($email,FILTER_VALIDATE_EMAIL)){

echo"email address is valid";}

else{

echo"email id is not valid";}}

?>

</body>

</html>

........

EX NO. : 11

DATE :

STUDENT MARKLIST

AIM :

 To write a php program to prepare the marklist using file handling.

PROGRAM :

<HTML>

<body>

<form action="" method="post">

<label for="regno">REGISTER No: </label>

<input type="text" name="regno"></br>

<label for="sname">Student Name: </label>

<input type="text" name="sname"></br>

<label for="dob">Student DOB:</label>

<input type="text" name="dob"></br>

<label for="class">Student Class:</label>

<input type="text" name="class"></br>

<label for="yos">Year:</label>

<input type="text" name="yos"></br>

<label for="sem">Semester:</label>

<input type="text" name="sem"></br>

<label for="m1">233IT:</label>

<input type="text" name="m1"></br>

<label for="m2">2332E:</label>

<input type="text" name="m2"></br>

<label for="m3">23BIT3C1:</label>

<input type="text"name="m3"></br>

<label for="m4">23DBS3A1:</label>

<input type="text"name="m4"></br>

<label for="m5">23BIT3S1:</label>

<input type="text"name="m5"></br>

<label for="m6">23BIT3S2:</label>

<input type="text"name="m6"></br>

<input type="submit" name="submit" value="Submit">

</form>

</body>

</html>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$regno = $_POST['regno'];

$sname = $_POST['sname'];

$dob = $_POST['dob'];

$class = $_POST['class'];

$yos = $_POST['yos'];

$sem = $_POST['sem'];

$m1 = $_POST['m1'];

$m2 = $_POST['m2'];

$m3 = $_POST['m3'];

$m4 = $_POST['m4'];

$m5 = $_POST['m5'];

$m6 = $_POST['m6'];

$tot = $m1 + $m2 + $m3 +$m4 + $m5 + $m6;

$avg = $tot / 6;

if ($m1 < 35 || $m2 < 35 || $m3 < 35 || $m4 < 35 || $m5 < 35 || $m6 < 35) {

$grad = "Fail";

}

else if ($avg >= 90) {

$grad = "Outstanding";

}elseif($avg >=76 && $avg < 89){

$grad = "Distinction";

} elseif ($avg >= 60 && $avg < 75) {

$grad = "First Class";

} elseif ($avg >= 50 && $avg < 60) {

$grad = "Second Class";

} else {

$grad = "Third Class";

}

$data =

"$regno,$sname,$dob,$class,$yos,$sem,$m1,$m2,$m3,$m4,$m5,$m6,$tot,$avg,$grad\n";

$file = fopen("std_data.txt", "a"); // Open file in append mode

if ($file) {

fwrite($file, $data); // Write data to the file

fclose($file); // Close the file

echo "Data has been saved successfully.<br>";

} else {

echo "Unable to open file for writing.";

}}

if (file_exists("std_data.txt")) {

echo "<h1 align=\"center\">ALL STUDENTS' MARK LIST</h1>";

echo "<table border=\"1\" align=\"center\">";

echo "<th>REGISTER NO: </th>” ;

echo “<th>NAME: </th>”;

echo “<th>DATE OF BIRTH: </th>”;

echo “<th>CLASS: </th>”;

echo “<th>YEAR: </th>”;

echo “<th>SEMESTER: </th>”;

echo “<th>Tamil</th>”;

echo “<th>English</th>”;

echo “<th>Major</th>”;

echo “<th>Allied</th>”;

echo “<th>Skilled 1</th>”;

echo “<th>Skilled 2</th>”;

echo “<th>TOTAL</th>”;

echo “<th>AVERAGE</th>”;

echo “<th>GRADE</th>”;

echo “</tr>”;

$file = fopen(“std_data.txt”, “r”);

while (($line = fgets($file)) !== false) {

$student = explode(“,”, trim($line));

echo “<tr align=\”center\”>”;

foreach ($student as $value) {

echo “<td>” . htmlspecialchars($value) . “</td>”;

}

echo “</tr>”;

}

fclose($file);

echo “</table>”;

}

?>

.......

EX.NO: 12

DATE :

EB BILL

AIM:

 To write a program to prepare the EB bill using file handling.

PROGRAM:

<html>

<head>

<title>CALCULATE ELECTRICITY BILL</title>

</head>

<?php

$result_str=$result='';

if(isset($_POST['unit-submit'])){

$units=$_POST['units'];

if(!empty($units)){

$result=calculate_bill($units);

$result_str='total amount of'.$units.':'.$result;}}

functioncalculate_bill($units){

$unit_cost_first=3.50;

$unit_cost_second=4.00;

$unit_cost_third=5.20;

$unit_cost_fourth=6.50;

if($units<=50){

$bill=$units*$unit_cost_first;}

elseif($units>50&&$units<=100){

$temp=50*$unit_cost_first;

$remaining_units=$units-50;

$bill=$temp+($remaining_units*$unit_cost_second);}

elseif($units>100&&$units<=200){

$temp=(50*3.5)+(100*$unit_cost_second);}

elseif($units>100&&$units<=200){

$temp=(50*3.5)+(100*$unit_cost_second);

$remaining_units=$units-150;

$bill=$temp+($remaining_units*$unit_cost_third);}

else{

$temp(50*3.5)+(100*$unit_cost_second)+(100*$unit_cost_third);

$remaining_units=$units-250;

$bill=$temp+($remaining_inits*$unit_cost_fourth);}

returnnumber_format((float)$bill,2,'.','');}

?>

<body>

<div id="page-wrap">

<h1>CALCULATE ELECTRICITY BILL</h1>

<form action=""method="post"id="quiz-form">

<input type="number"name="units"id="units"placeholder="please enter no.of units"/>

<input type="submit"name="unit-submit"id="unit-submit"value="submit"/>

</form><div>

<?php echo'<br/>'.$result_str;?>

</div></div>

</body>

</html>

.......

EX NO. : 13

DATE :

SALARY BILL

AIM :

 To write a php program to prepare the salary bill using file handling .

PROGRAM :

<html><body>

<form action="" method="post">

Employee ID: <input type="text" name="emp_id" required><br>

Employee Name: <input type="text" name="emp_name" required><br>

Basic Salary: <input type="number" name="basic_salary" required><br>

Allowances: <input type="number" name="allowances" required><br>

Deductions: <input type="number" name="deductions" required><br>

<input type="submit" value="Generate Salary Slip">

</form></body></html>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$emp_name = $_POST['emp_name'];

$emp_id = $_POST['emp_id'];

$basic_salary = $_POST['basic_salary'];

$allowances = $_POST['allowances'];

$deductions = $_POST['deductions'];

$net_salary = $basic_salary + $allowances - $deductions;

$data = "Employee ID: $emp_id\n";

$data .= "Employee Name: $emp_name\n";

$data .= "Basic Salary: $basic_salary\n";

$data .= "Allowances: $allowances\n";

$data .= "Deductions: $deductions\n";

$data .= "Net Salary: $net_salary\n";

$data .= "--------------------------\n";

$file = fopen("salary.txt", "a");

if ($file) {

fwrite($file, $data);

fclose($file);

echo "Salary data has been saved successfully.<br>";

} else {

echo "Unable to open file for writing.";

}

if (file_exists("salary.txt")) {

echo "<h1 align=\"center\">SALARY SLIP</h1>";

echo "<table border=\"1\" align=\"center\">";

echo "Employee ID: ". $emp_id. "<br/>";

echo "Employee Name: ". $emp_name. "<br/>";

echo "Basic Salary: ". $basic_salary."<br/>";

echo "Allowances: ".$allowances ."<br/>";

echo "Deductions: " . $deductions."<br/>";

echo "Net Salary: " . $net_salary . "<br/>";

$file = fopen("salary.txt", "r");

while (($line = fgets($file)) !== false) {

$employee = explode(",", trim($line));

echo "<tr align=\"center\">";

foreach ($employee as $value) {

echo "<td>" . htmlspecialchars($value) . "</td>";

}

echo "</tr>";

}

fclose($file);

echo "</table>";

}}

?>

.......

EX NO. : 14

DATE :

COPY A FILE

AIM :

 To write a php program to copy a file and implement with exception handling techniques

PROGRAM :

<?php

function copyFile($sourceFile, $destinationFile) {

try {

if (!file_exists($sourceFile)) {

throw new Exception("Source file does not exist.");

}

if (!copy($sourceFile, $destinationFile)) {

throw new Exception("Error copying the file.");

}

echo "File copied successfully.";

} catch (Exception $e) {

echo "An error occurred: " . $e->getMessage();

}

}

$sourceFile = "students_data.txt";

$destinationFile = "salary_data.txt";

copyFile($sourceFile, $destinationFile);

........

EX NO. : 15

DATE :

SESSION MANAGEMENT

AIM :

 To write the php program to implement the session management.

PROGRAM :

<?php

session_start();

?>

<!DOCTYPE html>

<html>

<body>

<?php

$_SESSION["favcolor"] = "green";

$_SESSION["favanimal"] = "cat";

echo "Session variables are set.";

?>

</body>

</html>

<?php

session_start ();

?>

<!DOCTYPE html>

<html>

<body>

<?php

echo "Favoritecolor is ". $_SESSION["favcolor"]. ". <br>";

echo "Favorite animal is ". $_SESSION["favanimal"]. ".";

?>

</body></html>

.......

EX NO. : 16

DATE :

COOKIES

AIM :

 To write a php program to implement the cookies concept.

PROGRAM :

COOKIE1.PHP

<?php

setcookie("Auction_Item", "Luxury Car", time() + 2 * 24 * 60 * 60);

?>

<html>

<body>

<?php

echo "cookie is created."

?>

</body>

</html>

COOKIE2.PHP

<html>

<body>

<?php

echo "Auction Item is a " . $_COOKIE["Auction_Item"];

?>

</body>

</html>

Comments