Professional Documents
Culture Documents
INDEX
Ex no: 1
String Manipulation
Date: 09.02.2022
Aim:
Algorithm:
Open a Notepad.
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.
<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:
Algorithm:
Open a Notepad.
Module(%), Exponential(**).
<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
Aim:
Algorithm:
Open a Notepad.
<?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
Aim:
Algorithm:
Open a Notepad.
<html>
<body>
<form>
</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;
Output:
Result
Thus the program was executed successfully and the output was verified.
Ex no:5
Aim:
Algorithm:
Open a Notepad.
The count increases at every time when the php program will be run.
<?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
Aim:
Algorithm:
<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:
Algorithm:
Open a Notepad.
Create a text file and write some content into it. Save it by using .txt extension.
<?php
fclose($handle);//close file
fclose($fp);
?>
Output:
Result:
Thus the program was executed successfully and the output was verified.
Ex no:8
Aim:
Algorithm:
Open a Notepad.
Create a HTML form using the text box, label and submit button.
<?php
if (isset($_POST['submit']))
{
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$email = $_POST['email'];
$password = $_POST['password'];
?>
<!DOCTYPE html>
<html>
<body>
<h2>Registration Form</h2>
<fieldset>
<legend>Information:</legend>
First name:<br>
<br>
Last name:<br>
<br>
Email:<br>
<br>
Password:<br>
<br>
<br>
</fieldset>
</form>
</body>
</html>
Output:
Result:
Thus the program was executed successfully and the output was verified.
Ex no:9
Aim:
Algorithm:
Open a Notepad.
<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>
$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;
// 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
Aim:
Algorithm:
Open a Notepad.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mysql";
if (isset($_POST['submit']))
{
$Student_ID = $_POST['studid'];//Textbox1
$Student_name = $_POST['sname'];//Textbox2
VALUES ('$Student_ID','$Student_name')";
$result = $conn->query($sql);
if ($result == TRUE)
{
}
else
{
?>
<!DOCTYPE html>
<html>
<body>
<h2>Student Info</h2>
<fieldset>
<legend>Information:</legend>
Student ID:<br>
<br>
Student name:<br>
<br>
</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:
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.
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();
$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.