PHP AND MY SQL LABSET
1.WRITE A PHP SCRIPT TO SWAP TWO NUMBERS.
<HTML>
<HEAD>
<TITLE>SWAP TWO NUMBERS</TITLE>
</HEAD>
<BODY>
<?php
// Function to swap two numbers using call by value
function swapByValue($a, $b) {
$temp = $a;
$a = $b;
$b = $temp;
echo "Swapped values using call by value: a = $a, b = $b\n";
}
// Function to swap two numbers using call by reference
function swapByReference(&$a, &$b) {
$temp = $a;
$a = $b;
$b = $temp;
echo "Swapped values using call by reference: a = $a, b =
$b\n";
}
// Main code
$a = 10;
$b = 20;
echo "Before swapping: a = $a, b = $b\n";
swapByValue($a, $b);
echo "After swapping using call by value: a = $a, b = $b\n";
swapByReference($a, $b);
echo "After swapping using call by reference: a = $a, b = $b\
n";
?>
</BODY>
</HTML>
2.WRITE A PHP SCRIPT TO FIND FACTORIAL OF A NUMBER.
<HTML>
<HEAD><TITLE>FACTORIAL OF A NUMBER</TITLE>
</HEAD>
<BODY>
<?php
// PHP code to get the factorial of a number
// function to get factorial in iterative way
function Factorial($number){
$factorial = 1;
for ($i = 1; $i <= $number; $i++){
$factorial = $factorial * $i;
}
return $factorial;
}
// Driver Code
$number = 10;
$fact = Factorial($number);
echo "Factorial = $fact";
?>
</BODY>
</HTML>
3.WRITE A PHP SCRIPT TO REVERSE A GIVEN NUMBER AND CALCULATE
THE SUM.
<HTML>
<HEAD><TITLE>REVERSE A NUMBER AND CALCULATE ITS
SUM</TITLE></HEAD>
<BODY>
<?php
// Function to reverse the number and calculate the sum of its digits
function reverseAndSum($number) {
$reversed = 0; // To store the reversed number
$sum = 0; // To store the sum of digits
while ($number > 0) {
// Get the last digit of the number
$digit = $number % 10;
// Add the digit to the sum
$sum += $digit;
// Add the digit to the reversed number
$reversed = $reversed * 10 + $digit;
// Remove the last digit from the number
$number = (int)($number / 10);
}
return [$reversed, $sum];
}
// Example usage
$number = 12345;
list($reversed, $sum) = reverseAndSum($number);
echo "Original number: $number\n";
echo "Reversed number: $reversed\n";
echo "Sum of digits: $sum\n";
?>
</BODY>
</HTML>
4. WRITE A PHP SCRIPT TO GENERATE A FIBONACCI SERIES USING
RECURSIVE FUNCTION
<HTML>
<HEAD>
<TITLE> TO GENERATE A FIBONACCI SERIES USING RECURSIVE
FUNCTION</TITLE>
</HEAD>
<BODY>
<?php
// Recursive function to calculate Fibonacci number
function fibonacci($n) {
// Base cases
if ($n == 0) {
return 0;
} elseif ($n == 1) {
return 1;
} else {
// Recursive call
return fibonacci($n - 1) + fibonacci($n - 2);
}
}
// Example usage to print the first 10 Fibonacci numbers
for ($i = 0; $i < 10; $i++) {
echo fibonacci($i) . " ";
}
?>
</BODY>
</HTML>
5. WRITE A PHP SCRIPT TO IMPLEMENT CONSTRUCTOR AND
DESTRUCTOR
<html>
<head>
<title>constructor and destructor</title>
</head>
<body>
<?php
class Student
{
Public $name;
Public $rollno,$m1,$m2,$m3,$tm;
function __construct($name,$rollno,$m1,$m2,$m3)
{
$this->name=$name;
$this->rollno=$rollno;
$this->m1=$m1;
$this->m2=$m2;
$this->m3=$m3;
//$this->tm=$this->m1+$this->m2+$this->m3;
}
function __destruct()
{
echo "Destroying object";
echo"<br>";
}
function show_details()
{
echo "Name of Student is". $this->name;
echo "<br>";
echo"Roll number is".$this->rollno;
echo "<br>";
echo "Mark1 :" .$this->m1;
echo "<br>";
echo "Mark2 :".$this->m2;
echo "<br>";
echo "Mark3 :".$this->m3;
echo "<br>";
echo "Total Mark is :".$this->tm=$this->m1+$this->m2+$this->m3;
echo"<br>";
}
}
$emp=new Student("Rakesh",3,34,35,36);
$emp->show_details();
echo "<br>";
$employee2=new Student("Vikas",7,23,24,25);
$employee2->show_details();
?>
</body>
</html>
Output
Name of Student is Rakesh
Roll number is3
Mark1:34
Mark2:35
Mark3:36
Total Mark is: 105
Name of Student is Vikas
Roll number is7
Mark1:23
Mark2:24
Mark3:25
Total Mark is: 72
Destroying object
Destroying object
6. WRITE A PHP SCRIPT TO IMPLEMENT FORM HANDLING USING
GET METHOD.
HTML Form (form_get_method.html)
<!DOCTYPE html>
<html >
<head>
<title>Form Handling using GET Method</title>
</head>
<body>
<h2>Form Handling with GET Method</h2>
<form action="handle_get.php" method="GET">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="age">Age:</label>
<input type="number" id="age" name="age" required>
<button type="submit">Submit</button>
</form>
</body>
</html>
PHP Script to Handle the GET Request (handle_get.php)
This PHP script will handle the data sent via the GET method. It will display the
data submitted from the form.
handle_get.php
<?php
// Check if the GET request contains the data
if (isset($_GET['name']) && isset($_GET['email']) && isset($_GET['age']))
{
// Retrieve the form data from the GET request
$name = $_GET['name'];
$email = $_GET['email'];
$age = $_GET['age'];
// Display the form data
echo "<h2>Form Submission Details</h2>";
echo "<p><strong>Name:</strong> $name</p>";
echo "<p><strong>Email:</strong> $email</p>";
echo "<p><strong>Age:</strong> $age</p>";
}
else
{
// If data is not received, show an error message
echo "<p>No data received. Please go back and submit the form.</p>";
}
?>
7. WRITE A PHP SCRIPT TO IMPLEMENT FORM HANDLING USING
POST METHOD.
HTML Form (form_post_method.html)
Here is an HTML form that uses the POST method to send data to a PHP script.
html
<!DOCTYPE html>
<head>
<title>Form Handling using POST Method</title>
</head>
<body>
<h2>Form Handling with POST Method</h2>
<form action="handle_post.php" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="age">Age:</label>
<input type="number" id="age" name="age" required>
<button type="submit">Submit</button>
</form>
</body>
</html>
PHP Script to Handle the POST Request (handle_post.php)
This PHP script will handle the data sent via the POST method. It will display the
submitted data.
<?php
// Check if the POST request contains the required data
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['age'])) {
// Retrieve the form data from the POST request
$name = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];
// Display the form data
echo "<h2>Form Submission Details</h2>";
echo "<p><strong>Name:</strong> $name</p>";
echo "<p><strong>Email:</strong> $email</p>";
echo "<p><strong>Age:</strong> $age</p>";
} else {
// If the required data is not present, show an error message
echo "<p>No data received. Please go back and submit the form.</p>";
}
?>
8 .WRITE A PHP SCRIPT THAT RECEIVES FORM INPUT BY THE
METHOD POST TO CHECK THE NUMBER IS PRIME OR NOT
(prime_check_form.html)
<html >
<head>
<title>Prime Number Checker</title>
</head>
<body>
<h2>Prime Number Checker</h2>
<form action="check_prime.php" method="POST">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required>
<button type="submit">Check</button>
</form>
</body>
</html>
(check_prime.php)
This PHP script receives the number from the form, checks if it's a prime number,
and displays the result.
<?php
// Check if the form has been submitted and the 'number' is set
if (isset($_POST['number'])) {
// Retrieve the number from the POST request
$MyNum = (int)$_POST['number'];
$n = 0;
for($i = 2; $i < $MyNum; $i++) {
if($MyNum % $i == 0){
$n++;
break;
}
}
if ($n == 0){
echo $MyNum." is a prime number.";
} else {
echo $MyNum." is not a prime number.";
}
}
9. WRITE A PHP SCRIPT THAT RECEIVES STRING AS A FORM INPUT.
HTML Form (form.html)
< html>
<head>
<title>String Input Form</title>
</head>
<body>
<h2>Enter a String</h2>
<form action="process.php" method="POST">
<label for="inputString">Enter your string:</label>
<input type="text" id="inputString" name="inputString" required>
<button type="submit">Submit</button>
</form>
</body>
</html>
PHP Script (process.php)
<html>
<head></head>
<body>
<?php
// Check if the form has been submitted and input exists
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['inputString'])) {
// Retrieve the input string
$inputString = $_POST['inputString'];
// Process the string (e.g., display it back to the user)
echo "<h2>You entered: $inputString</h2>";
} else {
echo "<p>No input received.</p>";
}
?>
</body>
</html>
10. WRITE A PHP SCRIPT TO COMPUTE ADDITION OF TWO
MATRICES AS A FORM INPUT.
<!DOCTYPE html>
<html>
<head>
<title>Matrix Addition</title>
</head>
<body>
<h2>Matrix Addition</h2>
<form method="post">
<h3>Matrix A</h3>
<?php
$rows = 2;
$cols = 2;
for ($i = 0; $i < $rows; $i++) {
for ($j = 0; $j < $cols; $j++) {
echo "<input type='number' name='a[$i][$j]' required> ";
}
echo "<br>";
}
?>
<h3>Matrix B</h3>
<?php
for ($i = 0; $i < $rows; $i++) {
for ($j = 0; $j < $cols; $j++) {
echo "<input type='number' name='b[$i][$j]' required> ";
}
echo "<br>";
}
?>
<br>
<input type="submit" name="submit" value="Add Matrices">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$a = $_POST['a'];
$b = $_POST['b'];
$sum = [];
echo "<h3>Result (A + B):</h3>";
echo "<table border='1' cellpadding='10'>";
for ($i = 0; $i < $rows; $i++) {
echo "<tr>";
for ($j = 0; $j < $cols; $j++) {
$sum[$i][$j] = $a[$i][$j] + $b[$i][$j];
echo "<td>" . $sum[$i][$j] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html>
11. WRITE A PHP SCRIPT TO SHOW THE FUNCTIONALITY OF DATE
AND TIME FUNCTION.
<?php
// Get the current date and time
$current_date = date("Y-m-d");
$current_time = date("H:i:s");
$current_datetime = date("Y-m-d H:i:s");
// Format the date and time
$formatted_date = date("F j, Y", strtotime($current_date));
$formatted_time = date("h:i A", strtotime($current_time));
$formatted_datetime = date("F j, Y h:i A", strtotime($current_datetime));
// Print the results
echo "Current Date: " . $current_date . "<br>";
echo "Current Time: " . $current_time . "<br>";
echo "Current Datetime: " . $current_datetime . "<br>";
echo "<br>";
echo "Formatted Date: " . $formatted_date . "<br>";
echo "Formatted Time: " . $formatted_time . "<br>";
echo "Formatted Datetime: " . $formatted_datetime . "<br>";
// Demonstrating strtotime() function
$specific_date = strtotime("2025-06-01");
$formatted_specific_date = date("Y-m-d", $specific_date);
echo "<br>";
echo "Specific Date (from strtotime): " . $formatted_specific_date . "<br>";
// Example of adding time using strtotime()
$future_date = strtotime("+1 week", strtotime($current_date));
$formatted_future_date = date("Y-m-d", $future_date);
echo "<br>";
echo "Future Date (after +1 week): " . $formatted_future_date . "<br>";
?>
Explanation:
1. 1. date() function:
The date() function is used to format the current date and time. It takes two
arguments:
o The format string, which defines the desired output format (e.g., "Y-
m-d" for year-month-day, "H:i:s" for hour:minute:second).
o An optional timestamp, which defaults to the current server time if
not provided.
2. 2. strtotime() function:
The strtotime() function is used to convert a date string into a Unix timestamp
(the number of seconds since January 1, 1970). It can also be used to add or
subtract time from a date string.
3. 3. Example Usage:
o The code first gets the current date, time, and datetime using
the date() function.
o It then formats these values using different format strings
with date().
o It demonstrates the use of strtotime() to convert a date string into a
timestamp and vice-versa.
o It also shows how to use strtotime() to add time to a date string.
4. 4. Output:
The script will display the current date, time, and datetime, as well as the
formatted versions, and examples of how to use strtotime().
This script demonstrates various ways to use the date() and strtotime() functions
in PHP to work with date and time values.
12. WRITE A PHP SCRIPT TO IMPLEMENT DATABASE CREATION.
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE college";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
13. DEVELOP A PHP PROGRAM TO DESIGN A COLLEGE ADMISSION
FORM USING MYSQL DATABASE
CREATE DATABASE college_admission;
USE college_admission;
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
phone VARCHAR(15) NOT NULL,
date_of_birth DATE NOT NULL,
gender ENUM('Male', 'Female', 'Other') NOT NULL,
course VARCHAR(100) NOT NULL,
address TEXT NOT NULL
);
. HTML Form (admission_form.html)
<!DOCTYPE html>
<html>
<title>College Admission Form</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h2>College Admission Form</h2>
<form action="submit_form.php" method="POST">
<label for="first_name">First Name:</label>
<input type="text" id="first_name" name="first_name" required>
<label for="last_name">Last Name:</label>
<input type="text" id="last_name" name="last_name" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="phone">Phone Number:</label>
<input type="text" id="phone" name="phone" required>
<label for="dob">Date of Birth:</label>
<input type="date" id="dob" name="dob" required>
<label for="gender">Gender:</label>
<select id="gender" name="gender" required>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select>
<label for="course">Course:</label>
<input type="text" id="course" name="course" required>
<label for="address">Address:</label>
<textarea id="address" name="address" required></textarea>
<button type="submit" name="submit">Submit</button>
</form>
</div>
</body>
</html>
PHP Script to Handle Form Submission (submit_form.php)
<?php
// Database connection
$servername = "localhost";
$username = "root"; // your MySQL username
$password = ""; // your MySQL password
$dbname = "college_admission";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Check if the form is submitted
if (isset($_POST['submit'])) {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$dob = $_POST['dob'];
$gender = $_POST['gender'];
$course = $_POST['course'];
$address = $_POST['address'];
// Prepare SQL query to insert data into database
$sql = "INSERT INTO students (first_name, last_name, email, phone, dob,
gender, course, address)
VALUES ('$first_name', '$last_name', '$email', '$phone', '$dob', '$gender',
'$course', '$address')";
// Execute the query and check for errors
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
// Close the connection
$conn->close();
?>