Php Lab Pgms
Php Lab Pgms
By Innahai Anugraham
pg. 1
3 Write a PHPscript to find maximum of three numbers.
<!DOCTYPE>
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n1= $_POST["number1"];
$n2= $_POST["number2"];
$n3= $_POST["number3"];
if ($n1 >= $n2 && $n1 >= $n3)
echo "$n1 is the maximum of $n1,$n2,$n3";
else if($n2 >= $n3)
echo "$n2 is the maximum of $n1,$n2,$n3";
else
echo "$n3 is the maximum of $n1,$n2,$n3";
}
?>
<h1>Maximum of Three Numbers</h1>
<form method="POST">
Enter Number1: <input type="text" name="number1" required><br/>
Enter Number2: <input type="text" name="number2" required><br/>
Enter Number3: <input type="text" name="number3" required><br/>
<input type="submit" value="Find Maximum">
</form>
</body>
</html>
Output:
pg. 2
echo "n1=".$n1." n2=".$n2;
$third = $n1;
$n1 = $n2;
$n2 = $third;
echo "<br><br>After swapping:<br><br>";
echo "n1=".$n1." n2=".$n2;
}
?>
<h1>Swapping Two Numbers</h1>
<form method="POST">
Enter Number1: <input type="text" name="number1" required><br/><br/>
Enter Number2: <input type="text" name="number2" required><br/>
<input type="submit" value="SWAP NUMBERS">
</form>
</body>
</html>
Ouput:
pg. 3
Enter Number: <input type="text" name="number" required><br/>
<input type="submit" value="Find Factorial">
</form>
</body>
</html>
Output:
pg. 4
Output:
7.Write a PHP script to reverse a given number and calculate its sum
<!DOCTYPE>
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$num= $_POST["number"];
$sum=0;
$rev=0;
$x=$num;
while( floor($num))
{
$rem=$num % 10;
$num=$num / 10;
$rev=$rev * 10 + $rem;
$sum=$sum + $rem;
}
pg. 5
8.Write a PHP script to to generate a Fibonacci series using Recursive function.
<!DOCTYPE>
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$num= $_POST["number"];
echo "<h3>Fibonacci series using recursive function:</h3>";
echo "\n";
/* Recursive function for fibonacci series. */
function series($num){
if($num == 0){
return 0;
}else if( $num == 1){
return 1;
} else {
return (series($num-1) + series($num-2));
}
}
/* Call Function. */
for ($i = 0; $i < $num; $i++){
echo series($i);
echo "\n";
}
}
?>
<h1>Fibonacci Series</h1>
<form method="POST">
Enter Number: <input type="text" name="number" required><br/><br>
<input type="submit" value="Enter End of Series">
</form>
</body>
</html>
Output:
pg. 6
9.Write a PHP script to implement atleast seven string functions.
<?php
echo”<h1> String Operations </h1>”;
$text = "Hello, World!";
$length = strlen($text);
echo "The length of the string $text is $length characters. <br><br>";
$pos = strpos($text, "World");
echo "The word 'World' starts at position $pos. <br><br>";
$newText = str_replace("World", "Universe", $text);
echo "Replaced: $newText <br><br>";
$substring = substr($text, 0, 5);
echo "The first 5 characters are: $substring <br><br>";
$uppercase = strtoupper($text);
echo "Uppercase: $uppercase <br><br>";
$lowercase = strtolower($text);
echo "Lowercase: $lowercase <br><br>";
$reversed = strrev($text);
echo "Reversed: $reversed <br><br>";
?>
O/P:
10. Write a PHP program to insert new item in array on any position in PHP.
<!DOCTYPE>
<html>
<body>
<?php
//Original Array on which operations is to be perform
$original_array = array( '1', '2', '3', '4', '5' );
echo 'Original array :';
foreach ($original_array as $x)
{
echo "$x ";
}
echo "\n";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//value of new item
pg. 7
$inserted_value = $_POST["number"];
//value of position at which insertion is to be done
$position = $_POST["pos"];
//array_splice() function
array_splice( $original_array, $position, 0, $inserted_value );
echo "<br><br>After inserting $inserted_value in the array position $position is :";
foreach ($original_array as $x)
{
echo"$x ";
}
}
?>
<h1>Array Insertion</h1>
<form method="POST">
Enter Number: <input type="text" name="number" required><br/><br/>
Enter Position: <input type="text" name="pos" required><br/>
<input type="submit" value="Insert">
</form>
</body>
</html>
Output:
// Constructor
public function __construct($name,$salary){
pg. 8
echo("In constructor <br><br>");
$this->name=$name;
$this->salary=$salary;
}
// destructor
public function __destruct(){
echo "In Destructor<br>";
echo ("Name: $this->name and Salary: $this->salary");
}
}
// creating object
$employee_1 = new Employee("John","10000");
?>
Output
12. Write a PHP Script to implement form handling using GET method.
Getform.html
<!DOCTYPE html>
<html>
<h1>GET METHOD</h1>
<form method="GET" action="get.php">
Enter Name: <input type="text" name="name" required><br/><br/>
Enter Age: <input type="text" name="age" required><br/><br/>
<input type="submit" value="submit">
</form>
</body>
</html>
Get.php
<!DOCTYPE html>
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br/>
Your age is:<?php echo $_GET["age"]; ?>
</body>
</html>
Output
pg. 9
13. Write a PHP Script to implement form handling using GET method.
postform.html
<!DOCTYPE html>
<html>
<h1>POST METHOD</h1>
<form method="POST" action="post.php">
Enter Name: <input type="text" name="name" required><br/><br/>
Enter Age: <input type="text" name="age" required><br/><br/>
<input type="submit" value="submit">
</form>
</body>
</html>
post.php
<!DOCTYPE html>
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br/>
Your age is:<?php echo $_POST["age"]; ?>
</body>
</html>
Output
pg. 10
14. Write a PHP script that receive form input by the method post to check the number
is prime or not.
<!DOCTYPE>
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n= $_POST["number"];
$c=0;
for ($i = 1; $i <= $n; $i++)
{
if (($n % $i) == 0)
$c++;
}
if ($c == 2)
echo("$n is a Prime number<br/><br/>");
else
echo("$n is not a Prime number <br/><br/>");
}
?>
<h1>PRIME NUMBER CHECKING</h1>
<form method="POST">
Enter Number: <input type="text" name="number" required><br/>
<input type="submit" value="Find PRIME">
</form>
</body>
</html>
Output:
pg. 11
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n= $_POST["name"];
}
?>
</body>
</html>
Output:
16. Write a PHP script to compute addition of two matrices as a form input.
<!DOCTYPE html>
<html>
<body>
<form method='get'>
<input type='submit' name='submit' value='MATRIX ADDITION'>
</form>
<?php
if(isset($_GET['submit']))
{
$a=array(
array(1,2,3),
array(1,2,3),
array(1,2,3)
);
$b=array(
array(1,2,3),
array(1,2,3),
array(1,2,3)
);
print "\n Matrix 1: <br>";
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
echo $a[$i][$j]," ";
}
echo "<br>";
}
echo "\n Matrix 2: <br>";
pg. 12
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
echo $b[$i][$j]," ";
}
echo "<br>";
}
$sum=array();
echo "\n Sum of Matrices: <br>";
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
echo $a[$i][$j]+$b[$i][$j]," ";
}
echo "<br>";
}
}
?>
</body>
</html>
Output:
17 Write a PHP script to show the functionality of date and time function.
<!DOCTYPE>
<html>
<body>
<h1>Date function Implementation</h1>
<?php
pg. 13
echo "Time using date function: ".date("h:i:s");
echo "<br/>";
echo "Date and Time using date function: ".date("M,d,Y h:i:s A");
echo "<br/>";
echo "Time with am/pm:".date("h:i a");
echo "<br/>";
$timestamp = time();
echo("Timestamp is $timestamp");
echo "<br/>";
echo("Date with Timestamp:".date("F d, Y h:i:s A", $timestamp));
echo "<br/>";
echo "Timestamp using mktime:".mktime(23, 21, 50, 11, 25, 2017);
?>
</body>
</html>
Output:
pg. 14
<h1> FILE UPLOADING </h1>
<form method="post" enctype="multipart/form-data">
Select File:
<input type="file" name="fileToUpload"/>
<input type="submit" value="Upload Image" name="submit"/>
</form>
</body>
</html>
Output:
pg. 15
20 Write a PHP script to create table
<?php
$host = 'localhost:3306';
$user = 'root';
$pass = '';
$dbname = 'test';
21. Develop a PHP program to design a college admission form using MYSQL database.
<html>
<head>
</head>
<body>
<h2>College Admission Form</h2>
<form method="post" >
<label>Name:</label><br>
<input type="text" name="name" required><br><br>
<label>Email:</label><br>
<input type="email" name="email" required><br><br>
<label>Phone:</label><br>
<input type="text" name="phone" required><br><br>
pg. 16
<label>Course:</label><br>
<select name="course">
<option value="Computer Science">Computer Science</option>
<option value="Engineering">Engineering</option>
<option value="Mathematics">Mathematics</option>
</select><br><br>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$host = 'localhost';
$user = 'root';
$pass = '';
$dbname='test';
if(!$conn){
die('Could not connect: '.mysqli_connect_error());
}
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$course = $_POST['course'];
</body>
</html>
OUTPUT:
1. Create table “admissions” inside database “test” with columns-
>student_name,email,phone,course.
pg. 17
Record in Database:
pg. 18