PHP_LAB_PROGRAMS
PHP_LAB_PROGRAMS
<?php
echo "Hello, World!";
?>
-----------------------------------------------------------------------------------
----------------------------------
2. Write a PHPscript to find odd or even number from given number.
<?php
// PHP script to check if a number is odd or even
ber
$number = 10; // You can change this value
<?php
$num1 = 12;
$num2 = 45;
$num3 = 29;
-----------------------------------------------------------------------------------
----------------------------------
<?php
// PHP script to swap two numbers
-----------------------------------------------------------------------------------
----------------------------------
5. Write a PHP script to find the factorial of a number.
<?php
// PHP script to find the factorial of a number
// Initialize factorial as 1
$factorial = 1;
-----------------------------------------------------------------------------------
--------------
7. Write a PHP script to reverse a given number and calculate its sum
<?php
// Function to reverse a number
function reverseNumber($number) {
$reversed = strrev(strval($number)); // Reverse the number as a string
return intval($reversed); // Convert the reversed string back to an integer
}
// Function to calculate the sum of the original number and its reversed version
function calculateSum($number) {
$reversed = reverseNumber($number);
return $number + $reversed; // Sum of original and reversed number
}
-----------------------------------------------------------------------------------
-----------------
8. Write a PHP script to to generate a Fibonacci series using Recursive function
<?php
// Recursive function to calculate Fibonacci number at a given position
function fibonacci($n) {
if ($n <= 1) {
return $n; // Base cases: return 0 for n=0 and 1 for n=1
}
return fibonacci($n - 1) + fibonacci($n - 2); // Recursive calculation
}
// Example usage
$count = 10; // Number of terms in the series
$fibonacciSeries = generateFibonacciSeries($count);
-----------------------------------------------------------------------------------
-----------------
9. Write a PHP script to implement atleast seven string functions.
<?php
// Original string
$originalString = "Hello, World!";
?>
-----------------------------------------------------------------------------------
---------------------------------------
10. Write a PHP program to insert new item in array on any position in PHP.
<?php
// Function to insert an item into an array at a specific position
function insertAtPosition($array, $item, $position) {
// Use array_splice to insert the item
array_splice($array, $position, 0, $item);
return $array;
}
// Example usage
$originalArray = [10, 20, 30, 40, 50]; // Original array
$newItem = 25; // Item to be inserted
$position = 2; // Desired position (index starts from 0)
-----------------------------------------------------------------------------------
---------------------------------------
11. Write a PHP script to implement constructor and destructor
<?php
// Define a class with a constructor and destructor
class SampleClass {
private $message;
// Example usage
$obj = new SampleClass("Hello, PHP!"); // Create an object of SampleClass
$obj->displayMessage(); // Call a method of the object
-----------------------------------------------------------------------------------
---------------------------------------
12. Write a PHP script to implement form handling using get method
<!DOCTYPE html>
<html>
<head>
<title>Form Handling with GET Method</title>
</head>
<body>
<!-- Form with GET method -->
<h2>Enter Your Details</h2>
<form method="GET" action="form_get.php">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br><br>
<button type="submit">Submit</button>
</form>
<?php
// Check if form data is submitted
if (!empty($_GET)) {
// Retrieve data from the GET request
$name = htmlspecialchars($_GET['name']); // Use htmlspecialchars to prevent
XSS
$email = htmlspecialchars($_GET['email']);
-----------------------------------------------------------------------------------
---------------------------------------
13. Write a PHP script to implement form handling using post method.
<!DOCTYPE html>
<html>
<head>
<title>Form Handling with POST Method</title>
</head>
<body>
<!-- Form with POST method -->
<h2>Enter Your Details</h2>
<form method="POST" action="form_post.php">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br><br>
<button type="submit">Submit</button>
</form>
<?php
// Check if form data is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve data from the POST request
$name = htmlspecialchars($_POST['name']); // Use htmlspecialchars to
prevent XSS
$email = htmlspecialchars($_POST['email']);
-----------------------------------------------------------------------------------
---------------------------------------
14. Write a PHP script that receive form input by the method post to check the
number is prime or not
<!DOCTYPE html>
<html>
<head>
<title>Check Prime Number</title>
</head>
<body>
<h2>Prime Number Checker</h2>
<form method="POST" action="check_prime.php">
<label for="number">Enter a Number:</label>
<input type="number" id="number" name="number" required>
<br><br>
<button type="submit">Check</button>
</form>
<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the number from the POST request
$number = intval($_POST['number']);
-----------------------------------------------------------------------------------
---------------------------------------
15. Write a PHP script that receive string as a form input
<!DOCTYPE html>
<html>
<head>
<title>String Input Form</title>
</head>
<body>
<h2>String Processor</h2>
<form method="POST" action="string_form.php">
<label for="inputString">Enter a String:</label>
<input type="text" id="inputString" name="inputString" required>
<br><br>
<button type="submit">Submit</button>
</form>
<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the string from the POST request
$inputString = htmlspecialchars($_POST['inputString']); // Sanitize input
-----------------------------------------------------------------------------------
---------------------------------------
16. 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" action="matrix_addition.php">
<h3>Matrix A:</h3>
<label>Row 1: </label>
<input type="number" name="a11" required>
<input type="number" name="a12" required>
<br>
<label>Row 2: </label>
<input type="number" name="a21" required>
<input type="number" name="a22" required>
<br>
<h3>Matrix B:</h3>
<label>Row 1: </label>
<input type="number" name="b11" required>
<input type="number" name="b12" required>
<br>
<label>Row 2: </label>
<input type="number" name="b21" required>
<input type="number" name="b22" required>
<br><br>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve values for Matrix A
$a11 = intval($_POST['a11']);
$a12 = intval($_POST['a12']);
$a21 = intval($_POST['a21']);
$a22 = intval($_POST['a22']);
17. Write a PHP script to show the functionality of date and time function.
<?php
// Display the current date and time
echo "Current Date and Time: " . date("Y-m-d H:i:s") . "\n";
-----------------------------------------------------------------------------------
-----------------------------------------------
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<h2>Upload a File</h2>
<form method="POST" action="file_upload.php" enctype="multipart/form-data">
<label for="file">Choose a file:</label>
<input type="file" name="file" id="file" required>
<br><br>
<button type="submit">Upload</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Check if the file was uploaded without errors
if (isset($_FILES["file"]) && $_FILES["file"]["error"] == 0) {
$fileName = $_FILES["file"]["name"];
$fileType = $_FILES["file"]["type"];
$fileSize = $_FILES["file"]["size"];
$tempPath = $_FILES["file"]["tmp_name"];
-----------------------------------------------------------------------------------
-----------------------------------------------
19. Write a PHP script to implement database creation
<?php
// Database connection credentials
$host = "localhost"; // Hostname (use "127.0.0.1" or "localhost")
$username = "root"; // MySQL username
$password = ""; // MySQL password
$dbName = "test_db"; // Name of the database to create
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully to MySQL server.<br>";
-----------------------------------------------------------------------------------
-----------------------------------------------
20. Write a PHP script to create table
<?php
// Database connection credentials
$host = "localhost"; // Hostname (e.g., "127.0.0.1" or "localhost")
$username = "root"; // MySQL username
$password = ""; // MySQL password
$dbName = "test_db"; // Existing database name
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully to the database '$dbName'.<br>";
-----------------------------------------------------------------------------------
-----------------------------------------------
21. Develop a PHP program to design a college admission form using MYSQL database.
USE college_admission;
<!DOCTYPE html>
<html>
<head>
<title>College Admission Form</title>
</head>
<body>
<h2>College Admission Form</h2>
<form method="POST" action="admission_form.php">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br><br>
<label for="phone">Phone:</label>
<input type="text" id="phone" name="phone" required>
<br><br>
<label for="dob">Date of Birth:</label>
<input type="date" id="dob" name="dob" required>
<br><br>
<label for="course">Course:</label>
<input type="text" id="course" name="course" required>
<br><br>
<button type="submit">Submit</button>
</form>
<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Database connection details
$host = "localhost";
$username = "root";
$password = "";
$dbName = "college_admission";
// Create a connection
$conn = new mysqli($host, $username, $password, $dbName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}