0% found this document useful (0 votes)
133 views54 pages

PHP Proram

The document contains details of 12 exercises completed as part of a PHP training program. Each exercise contains the date, topic covered, aim, algorithm, program coding, and output. The topics covered include string manipulations, math operations, array functions, looping structures, page hit counter, input/output operations, reading/writing files and directories, HTML forms, events calendar, MySQL connectivity and database manipulations, and sessions maintenance.

Uploaded by

Kaleeswari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views54 pages

PHP Proram

The document contains details of 12 exercises completed as part of a PHP training program. Each exercise contains the date, topic covered, aim, algorithm, program coding, and output. The topics covered include string manipulations, math operations, array functions, looping structures, page hit counter, input/output operations, reading/writing files and directories, HTML forms, events calendar, MySQL connectivity and database manipulations, and sessions maintenance.

Uploaded by

Kaleeswari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 54

S.

No Date Content Page No Signature

1 09.02.2022 String Manipulations

2 16.02.2022 Math Operations


3 23.02.2022 Array functions and Operations

4 02.03.2022 Looping Structures Utilization

5 09.03.2022 Page hit Counter

6 16.03.2022 Input / Output Operations

7 07.04.2022 Reading / Writing files and Directories

8 18.04.2022 Implement a PHP program to process


HTML forms
9 25.04.2022 Events calendar application using PHP

10 02.05.2022 MySQL Connectivity and Database


Manipulations

11 10.05.2022 Sessions maintenance in PHP

INDEX
Ex no: 1
String Manipulation
Date: 09.02.2022

Aim:

To write a PHP program to perform String Manipulation.

Algorithm:

 Open a Notepad.

 Start the Program.

 Write a PHP code to perform the following String Operations

 Lowercase to uppercase using strtolower() method.

 Uppercase to lowercase using strtoupper() method.

 Reverse the word using strrev() method.

 Change the first letter of the word to uppercase using ucfirst() method.

 Change the lower letter of the word to lowercase using lcfirst() method.

 Save the program with using. php extension inside the Xampp-htdocs folder.

 Run the program in browser window by using Localhost

 Finally, the output will be displayed.

 End the Program.


Program Coding:

<html>
<?php
if(isset($_POST['lower']))
{
$a=$_POST['fstr'];
$b=strtolower($a);
echo "Result:<input type='text' value='$b'/>";
}
if(isset($_POST['upper']))
{
$a=$_POST['fstr'];
$b=strtoupper($a);
echo "Result:<input type='text' value='$b'/>";
}
if(isset($_POST['reverse']))
{
$a=$_POST['fstr'];
$b=strrev($a);
echo "Result:<input type='text' value='$b'/>";
}
if(isset($_POST['ucfirst']))
{
$a=$_POST['fstr'];
$b=ucfirst($a);
echo "Result:<input type='text' value='$b'/>";
}

if(isset($_POST['lcfirst']))
{
$a=$_POST['fstr'];
$b=lcfirst($a);
echo "Result:<input type='text' value='$b'/>";
}
if(isset($_POST['slash']))
{
$a=$_POST['fstr'];
$b=addcslashes($a,'a..z');
echo "Result:<input type='text' value='$b'/>";
}
?>
<body>
<form method="post">
Enter the string <input type="text" name="fstr"/><hr/>
<input type="submit" name="lower" value="Lowercase"/>
<input type="submit" name="upper" value="uppercase"/>
<input type="submit" name="reverse" value="Reverse"/>
<input type="submit" name="ucfirst" value="Uppercase first"/>
<input type="submit" name="lcfirst" value="Lowercase first"/>
<input type="submit" name="slash" value="Slash"/>
</form>
</body>
</html>
Output:
Result:
Thus the program was executed successfully and the output was verified.
Ex no:2
Date: 16.02.2022 Math Operations

Aim:

To write a PHP program to perform Math Operations.

Algorithm:

 Open a Notepad.

 Start the Program.

 Code to perform math operations like addition(+), subtraction(-), Division(/), Multiplication(*),

Module(%), Exponential(**).

 To get the first number and second number.

 Check all math operations one by one.

 Save the program with using .php extension.

 Run the program in browser window by using Localhost

 Finally, the output will be displayed.

 End the Program.


Program coding:

<html>
<head>
<title>
Math Operation
</title>
</head>
<?php
if(isset($_POST['add']))
{
$a=$_POST['fnum'];
$b=$_POST['snum'];
$c=$a + $b;
echo "Result:<input type='text' value='$c'/>";
}
if(isset($_POST['sub']))
{
$a=$_POST['fnum'];
$b=$_POST['snum'];
$c=$a - $b;
echo "Result:<input type='text' value='$c'/>";
}
if(isset($_POST['multiple']))
{
$a=$_POST['fnum'];
$b=$_POST['snum'];
$c=$a * $b;
echo "Result:<input type='text' value='$c'/>";
}
if(isset($_POST['divide']))
{
$a=$_POST['fnum'];
$b=$_POST['snum'];
$c=$a / $b;
echo "Result:<input type='text' value='$c'/>";
}
if(isset($_POST['modulo']))
{
$a=$_POST['fnum'];
$b=$_POST['snum'];
$c=$a % $b;
echo "Result:<input type='text' value='$c'/>";
}
if(isset($_POST['exp']))
{
$a=$_POST['fnum'];
$b=$_POST['snum'];
$c=$a ** $b;
echo "Result:<input type='text' value='$c'/>";

}
?>
<body>
<form method="post">
Enter the first number <input type="text" name="fnum"/><hr/>
Enter the second number <input type="text" name="snum"/><hr/>
<input type="submit" name="add" value="Addition"/>
<input type="submit" name="sub" value="Subtraction"/>
<input type="submit" name="multiple" value="Multiplication"/>
<input type="submit" name="divide" value="Division"/>
<input type="submit" name="modulo" value="modulo"/>
<input type="submit" name="exp" value="Exponentiation"/>
</form>
</body>
</html>
Output:
Result:
Thus the program was executed successfully and the output was verified.
Ex no:3

Date: 23.02.2022 Array function and Operations

Aim:

To write a PHP program to perform Array function and Operation.

Algorithm:

 Open a Notepad.

 Start the program.

 Code to get the array elements.

 Code to perform the following array operations

 Arrange the array elements in ascending order and descending order.

 Find the size of the array.

 Calculate the sum of an array.

 Save the program with using .php extension.

 Run the program in Command Prompt.

 Finally, the output will be displayed.

 End the Program.


Program coding:

<?php
echo "\n 1. Ascending Order \n";
echo "\n 2. Descending Order \n";
echo "\n 3. Size of an array \n";
echo "\n 4. Sum of an array \n";
g1:
$a = (int)readline('Enter your choice: ');
switch($a)
{
case 1:
$arr = array();
for ($i = 0; $i < 5; $i++)
$arr[$i] = (int)readline('Enter the Array element: ');
sort($arr);
echo "\nThe Ascending Order\n";
echo "********************\n";
foreach($arr as $value)
{
echo $value . "\n";
}
break;
case 2:
$arr = array();
for ($i = 0; $i < 5; $i++)
$arr[$i] = (int)readline('Enter the Array element: ');
$reversearr=array_reverse($arr);
echo "\nThe Descending Order\n";
echo "********************\n";
foreach($reversearr as $value)
{
echo $value . "\n";
}
break;
case 3:
$arr = array();
for ($i = 0; $i < 5; $i++)
$arr[$i] = (int)readline('Enter the Array element: ');
echo "\n\nSize of the array is: ". sizeof($arr),"\n";
break;
case 4:
$arr = array();
for ($i = 0; $i < 5; $i++)
$arr[$i] = (int)readline('Enter the Array element: ');
$sum=array_sum($arr);
echo "<strong>Sum of array elements = $sum</strong><br>";
break;
}
echo "\n";
$b = (string)readline('Do you want to continue(Y/N)?');
if($b=="Y")
{
goto g1;
}
else
{
exit();
}

?>
Output:

Result:
Thus the program was executed successfully and the output was verified.
Ex no:4

Date:02.03.2022 Looping Structures Utilization

Aim:

To write a PHP program Looping Structures Utilization.

Algorithm:

 Open a Notepad.

 Start the program.

 Code to perform sum of digits using while looping structure.

 Save the program by using .php extension.

 Run the program in browser window by using Localhost

 Finally, the output will be displayed.

 End the Program.


Program coding:

<html>

<body>

<h1><center> LOOPING STRUCTURE UTILIZATION</center></h1>

<form>

Enter Your digit <input type="text" name="num"><br/>

<br/><input type="submit" value="find the sum">

</form>

</body>

</html>

<?php

@$num=$_GET['num'];

$sum=0;

$rem=0;

$len=0;

while((int)$num!=0)

$len++;

$rem=$num%10;

$sum=$sum+$rem;

$num=$num/10;

echo "Length of given digit= " . $len."<br/>";

echo "Sum of given digit= " . $sum;


?>

Output:
Result
Thus the program was executed successfully and the output was verified.
Ex no:5

Date: 09.03.2022 Page hit Counter

Aim:

To write a PHP program to page hit counter.

Algorithm:

 Open a Notepad.

 Start the program.

 Create an empty text document in notepad.

 Code to perform page hit counter operation

 Save this program by using .php extension.

 Run the program in browser window by using Localhost

 The count increases at every time when the php program will be run.

 End the Program.


Program Coding:

<?php
$file=fopen("visitors.txt","w");
session_start();
$_SESSION[ 'v' ]=$_SESSION[ 'v' ]+1;
$count=$_SESSION['v'];
fwrite($file,$count);
?>

<?php
$fi=file_get_contents("visitors.txt");
$count_read="Viewers:".$fi;
echo "This page has Page Hit Counter"."</br>";
echo "*****************************"."</br>";
echo $count_read;
?>
Output:
Result:
Thus the program was executed successfully and the output was verified.
Ex no:6

Date:16.03.2022 Input / Output Operations

Aim:

To write PHP program to perform Input / Output Operations.

Algorithm:

 Open the Notepad.

 Start the program.

 Code to create the form to display the student mark details.

 Get the marks and calculate total and average values.

 Save the file by using .php extension.

 Run the program in browser window by using Localhost

 Finally, the output will be displayed.

 End the Program.


Program Coding:

<html>
<head>
<title>
Student Mark Details
</title>
</head>

<body>
<form method ="POST">
Enter the Roll Number<input type="text"name="Rollnum"/><hr/>
Enter the StudentName<input type="text"name="Sname"/><hr/>
Enter the Tamil Mark<input type="text"name="Tnum"/><hr/>
Enter the English Mark<input type="text"name="Enum"/><hr/>
Enter the Maths Mark<input type="text"name="Mnum"/><hr/>
Enter the Science Mark<input type="text"name="Snum"/><hr/>
Enter the SS Mark<input type="text"name="SSnum"/><hr/>
<input type="submit"name="tot"value="Total">
<input type="submit"name="avg"value="Average">
</form>
</body>
</html>

<?php
if(isset($_POST['tot']))
{
$T=$_POST['Tnum'];
$E=$_POST['Enum'];
$M=$_POST['Mnum'];
$S=$_POST['Snum'];
$SS=$_POST['SSnum'];
$Total=$T+$E+$M+$S+$SS;
echo"result:<input type ='text' value='$Total'>";
}
if(isset($_POST['avg']))
{
$T=$_POST['Tnum'];
$E=$_POST['Enum'];
$M=$_POST['Mnum'];
$S=$_POST['Snum'];
$SS=$_POST['SSnum'];
$Avg=($T+$E+$M+$S+$SS)/5;
echo"result:<input type ='text'value='$Avg'>";
}
?>
Output:
Result:
Thus the program was executed successfully and the output was verified
Ex no:7
Reading / Writing files and Directories
Date:07.04.2022

Aim:

To write a PHP program Reading / Writing files and Directories.

Algorithm:

 Open a Notepad.

 Start the program.

 Create a text file and write some content into it. Save it by using .txt extension.

 PHP code to write a program to perform file operations.

 Run the program in browser window by using Localhost

 Finally, the output will be displayed.

 End the Program.


Program Coding:

<?php

$filename = "D:\\demo.txt ";

$handle = fopen($filename, "r");//open file in read mode

$contents = fread($handle, filesize($filename));//read file

echo $contents;//printing data of file

fclose($handle);//close file

$fp = fopen($filename, "a");//open file in write mode

fwrite($fp, 'Welcome to PHP Program ');

fclose($fp);

?>
Output:

Result:
Thus the program was executed successfully and the output was verified.
Ex no:8

Date:18.04.2022 Implement a PHP program to process HTML forms

Aim:

To write implement a PHP program to process HTML forms.

Algorithm:

 Open a Notepad.

 Start the program.

 Create a HTML form using the text box, label and submit button.

 Save the program by using .php extension.

 Run the program in browser window by using Localhost.

 Finally, the output will be displayed.

 End the Program.


Program Coding:

<?php

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

$first_name = $_POST['firstname'];

$last_name = $_POST['lastname'];

$email = $_POST['email'];

$password = $_POST['password'];

echo "Registration is successfully completed";


}

?>

<!DOCTYPE html>

<html>

<body>

<h2>Registration Form</h2>

<form action="" method="POST">

<fieldset>

<legend>Information:</legend>

First name:<br>

<input type="text" name="firstname">

<br>
Last name:<br>

<input type="text" name="lastname">

<br>

Email:<br>

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

<br>

Password:<br>

<input type="password" name="password">

<br>

<br>

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

</fieldset>

</form>

</body>

</html>
Output:

Result:
Thus the program was executed successfully and the output was verified.
Ex no:9

Date:25.04.2022 Events calendar application using PHP

Aim:

To write a program to perform events calendar application using PHP.

Algorithm:

 Open a Notepad.

 Start the program.

 Code to perform calendar application.

 Save the program by using .php extension.

 Run the program in browser window by using Localhost.

 Finally, the output will be displayed.

 End the Program.


Program Coding:

<style type="text/css">
table {
border:1px solid #aaa;
border-collapse:collapse;
background-color:#fff;
font-family: Verdana;
font-size:12px;
}

th {
background-color:#777;
color:#fff;
height:32px;
}

td {
border:1px solid #ccc;
height:32px;
width:32px;
text-align:center;
}

td.red {
color:red;
}

td.bg-yellow {
background-color:#ffffe0;
}
td.bg-orange {
background-color:#ffa500;
}

td.bg-green {
background-color:#90ee90;
}

td.bg-white {
background-color:#fff;
}

td.bg-blue {
background-color:#add8e6;
}

a{
color: #333;
text-decoration:none;

}
</style>

<table border='0' >


<?php
// Get the current date
$date = getdate();

// Get the value of day, month, year


$mday = $date['mday'];
$mon = $date['mon'];
$wday = $date['wday'];
$month = $date['month'];
$year = $date['year'];

$dayCount = $wday;
$day = $mday;

while($day > 0) {
$days[$day--] = $dayCount--;
if($dayCount < 0)
$dayCount = 6;
}

$dayCount = $wday;
$day = $mday;

if(checkdate($mon,31,$year))
$lastDay = 31;
elseif(checkdate($mon,30,$year))
$lastDay = 30;
elseif(checkdate($mon,29,$year))
$lastDay = 29;
elseif(checkdate($mon,28,$year))
$lastDay = 28;

while($day <= $lastDay) {


$days[$day++] = $dayCount++;
if($dayCount > 6)
$dayCount = 0;
}

// Days to highlight
$day_to_highlight = array(8, 9, 10, 11, 12, 22,23,24,25,26);

echo("<tr>");
echo("<th colspan='7' align='center'>$month $year</th>");
echo("</tr>");
echo("<tr>");
echo("<td class='red bg-yellow'>Sun</td>");
echo("<td class='bg-yellow'>Mon</td>");
echo("<td class='bg-yellow'>Tue</td>");
echo("<td class='bg-yellow'>Wed</td>");
echo("<td class='bg-yellow'>Thu</td>");
echo("<td class='bg-yellow'>Fri</td>");
echo("<td class='bg-yellow'>Sat</td>");
echo("</tr>");

$startDay = 0;
$d = $days[1];

echo("<tr>");
while($startDay < $d) {
echo("<td></td>");
$startDay++;
}

for ($d=1;$d<=$lastDay;$d++) {
if (in_array( $d, $day_to_highlight))
$bg = "bg-green";
else
$bg = "bg-white";
// Highlights the current day
if($d == $mday)
echo("<td class='bg-blue'><a href='#' title='Detail of day'>$d</a></td>");
else
echo("<td class='$bg'><a href='#' title='Detail of day'>$d</a></td>");

$startDay++;
if($startDay > 6 && $d < $lastDay){
$startDay = 0;
echo("</tr>");
echo("<tr>");
}
}
echo("</tr>");
?>
</table>
Output:

Result:
Thus the program was executed successfully and the output was verified.
Ex no:10

Date:02.05.2022 MySQL Connectivity and Database Manipulations

Aim:

To write a PHP program MySQL Connectivity and Database Manipulations.

Algorithm:

 Open a Notepad.

 Open the localhost/dashboard/.

 Click the phpmyadmin →mysql→new→table.

 Create the table and save it.

 Create a form using HTML tags.

 Code to write for MySQL Connectivity and Database Manipulations.

 Save the program by using. php extension.

 Run the program in browser window by using Localhost.

 Finally, the output will be displayed.

 End the Program.


Program Coding:

<?php

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "mysql";

$conn = new mysqli($servername, $username, $password, $dbname);

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

$Student_ID = $_POST['studid'];//Textbox1

$Student_name = $_POST['sname'];//Textbox2

$sql = "INSERT INTO `studs`(`sid`, `sname`)

VALUES ('$Student_ID','$Student_name')";

$result = $conn->query($sql);

if ($result == TRUE)
{

echo "New record created successfully.";

}
else
{

echo "Error:". $sql . "<br>". $conn->error;


}
$conn->close();

?>

<!DOCTYPE html>

<html>

<body>

<h2>Student Info</h2>

<form action="" method="POST">

<fieldset>

<legend>Information:</legend>

Student ID:<br>

<input type="text" name="studid">

<br>

Student name:<br>

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

<br>

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

</fieldset>

</form>

</body>

</html>
Output:
Result:
Thus the program was executed successfully and the output was verified
Ex no:11
Session maintenance in PHP
Date: 10.05.2022

Aim:

To write a PHP program to maintain the session.

Algorithm:

 Open a Notepad.

 Create a file named as Firstpage.php and write the session variable code into it.

 Create another file named as Secondpage.php and write the code to access session variable

into it.

 Run the Firstpage.php program at first in browser window by using Localhost.

 Finally, the output will be displayed.

 End the Program.


Program Coding:

First Page:

<?php
session_start();
$_SESSION["username"] = "Hi PHP";
$_SESSION["userid"] = "1";
?>

<html>
<body>

<?php
echo "Session variable is set.";
?>
<a href="second_page.php">Go to Second Page</a>

</body>

</html>

Second Page:

<?php
session_start();

// get the session variable values

$username = $_SESSION["username"];
$userid = $_SESSION["userid"];
?>

<html>
<body>

<?php
echo "Username is: ".$username."<br/>";
echo "User id is: ".$userid;
?>

</body>
</html>

Output:
Result:
Thus the program was executed successfully and the output was verified.

You might also like