PHP Labprograms BCA NEP
PHP Labprograms BCA NEP
Output :
Hello world
2. Write a PHP script to find odd or even number from given number.
<!DOCTYPE html>
<html>
<body>
<?php
$number = 21;
if ($number % 2 == 0) {
print "It's even";
}
else
{
print "It's odd";
}
?>
</body>
</html>
Output :
its Odd
3. Write a PHP script to find maximum of three numbers.
<!DOCTYPE html>
<html>
<body>
<?php
$number1 = 123;
$number2 = 72;
$number3 = 151;
$maxNumber = max($number1, $number2, $number3);
echo "The largest number among three is: $maxNumber\n";
?>
</body>
</html>
Output :
The largest number among three numbers is :151
Output :
Before swapping :
a=45
b=56
After Swapping :
a=56
b=45
Output :
Factorial = 120
6. Write a PHP script to check if number is palindrome or not.
<?php
$num = 12321;
$org_num=$num;
$revnum = 0;
while ($num != 0)
{
$revnum = $revnum * 10 + $num % 10;
//below cast is essential to round remainder towards zero
$num = (int)($num / 10); //0
}
if($revnum==$org_num)
{
echo $org_num," is Palindrome number";
}
else
{
echo $org_num." is not Palindrome number";
}
?>
Output :
12321 is palindrome number
7. Write a PHP script to reverse a given number and calculate its sum
<?php
function reverse_number($num) {
$rev_num = 0;
return $rev_num;
function calculate_sum($num) {
$sum = 0;
return $sum;
$num = 12345;
$reversed_num = reverse_number($num);
$sum = calculate_sum($reversed_num);
?>
Output :
The orginal number is 12345
The reversed number is 54321
The sum of the degits of reversed number is 15
8. Write a PHP script to generate a Fibonacci series using Recursive function
<?php
if($n == 0) {
return;
fibonacci($n);
?>
Output :
Fibonacci series :
01123
<?php
echo $str;
echo strlen($str);
echo strtolower($str);
echo strtoupper($str);
$part = substr($str,7,3);
echo "$part";
?>
Output :
Hello, PHP!
string length is : 11
<?php
$inserted_value = '11';
$position = 2;
//array_splice() function
?>
Output :
Original array :1 2 3 4 5
<?php
class Employee
Public $name;
Public $position;
function __construct($name,$position)
$this->name=$name;
$this->position=$position;
function show_details()
function __destruct() {
$employee_obj->show_details();
$employee2= new Employee("Vikas","Manager");
$employee2->show_details();
?>
Output :
Rakesh:your position is :Developer
Vikas:your position is :Manager
Inside destructor,
The name is Vikas and position is Manager,
Inside destructor,
The name is Rakesh and position is Developer,
12. Write a PHP script to implement form handling using get method
<!DOCTYPE html>
<html>
<body>
<h4>PHP Form Handling using GET method</h4>
<input type="submit">
</form>
<?php
<br/>
Your name is <?php echo $_GET["name"]; ?>
<br/>
Your age is <?php echo $_GET["age"]; ?>
<?php endif; ?>
</body>
</html>
Output :
13. Write a PHP script to implement form handling using post method
<!DOCTYPE html>
<html>
<body>
<input type="submit">
</form>
<?php
<br/>
<br/>
</body>
</html>
Output :
14.Write a PHP script that receive form input by the method post to check the
number is prime or not
<!DOCTYPE html>
<html>
<body>
<form method="post">
</form>
<?php
if($_POST)
$input=$_POST['input'];
if ($input % $i == 0) {
$value= True;
}
} else {
?>
</body>
</html>
Output :
<!DOCTYPE html>
<html>
<body>
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$string = $_POST['inputString'];
if (empty($string)) {
} else {
?>
</body>
</html>
Output :
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>
<h3>Matrix A</h3>
<label for="a11">A[1][1]:</label>
<label for="a12">A[1][2]:</label>
<label for="a21">A[2][1]:</label>
<label for="a22">A[2][2]:</label>
<h3>Matrix B</h3>
<label for="b11">B[1][1]:</label>
<label for="b12">B[1][2]:</label>
<label for="b21">B[2][1]:</label>
<label for="b22">B[2][2]:</label>
</form>
<?php
if (isset($_POST['submit'])) {
// Get the values from the form for Matrix A
$a11 = (int)$_POST['a11'];
$a12 = (int)$_POST['a12'];
$a21 = (int)$_POST['a21'];
$a22 = (int)$_POST['a22'];
$b11 = (int)$_POST['b11'];
$b12 = (int)$_POST['b12'];
$b21 = (int)$_POST['b21'];
$b22 = (int)$_POST['b22'];
echo "<tr><td>$c11</td><td>$c12</td></tr>";
echo "<tr><td>$c21</td><td>$c22</td></tr>";
echo "</table>";
?>
</body>
</html>
Output :
Write the numbers in
blank boxes
5 2 6 7
9 2 7 8
17. Write php script to show the functionality of date time function.
<?php
date_default_timezone_set('UTC');
echo "Specific date and time (March 10, 2024 14:30:00): " . date('Y-m-d H:i:s', $timestamp) . "\n";
?>
Output :
Current date and time :24-06-15 15:29:50
Current year :24
Current month :06
Current day :15
Current hour :15
Current minute :29
Current second :50
Current time slap :1718465390
specific date and time (March 10,2024 14:30:00) : 24-03-10 14:30:00
Next Monday :24-06-17 00:00:00
index.html
<!DOCTYPE html>
<html>
<body>
enctype="multipart/form-data">
id="fileToUpload"><br>
name="submit">
</form>
</body>
</html>
fileupload.php
<!DOCTYPE html>
<?php
$target_dir = $_POST["dirname"]."/";
$uploadOk = 1;
$extensions = array("jpeg","jpg","png","pdf","gif");
if(isset($_POST["submit"])) {
if(!empty($_POST["dirname"])){
if(!is_dir($_POST["dirname"])) {
mkdir($_POST["dirname"]);
$uploadOk = 1;
else {
echo "Specify the directory name...";
$uploadOk = 0;
exit;
$uploadOk = 1;
else {
$uploadOk = 0;
exit;
if (file_exists($target_file)) {
$uploadOk = 0;
exit;
$uploadOk = 0;
exit;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0)
else
if (move_uploaded_file($_FILES["fileToUpload"]
["tmp_name"], $target_file))
else
?>
</body>
</html>
Output :
19. Write a PHP script to implement database creation
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
// Check connection
if ($conn->connect_error) {
// Create database
} else {
$conn->close();
?>
Output :
Database created successfully
20. Write a PHP script to create table
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create database
} else {
$dbname = "empDB";
email VARCHAR(50),
)";
} else {
echo "<br> Error creating table: " . $conn->error;
$conn->close();
?>
Output :
21. Develop a PHP program to design a college admission form using MYSQL database.
Form.html
<!DOCTYPE html>
<html lang="en">
<body>
<p>
FirstName:
<span style="color:red;">*</span>
<br>
<br>
</p>
<p>
LastName:
<br>
<br>
</p>
<p>
<label for="Gender">Gender:</label>
<span style="color:red;">*</span>
</p>
<p>
Address:
<span style="color:red;">*</span>
<br>
<br>
</p>
<p>
Email:
<span style="color:red;">*</span>
<br>
<br>
</p>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>
insert.php
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<center>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "admission_DB";
// Check connection
. mysqli_connect_error());
}
} else {
$conn->select_db($dbname);
email VARCHAR(50)
)";
} else {
}
// Taking all 5 values from the form data(input)
$first_name = $_REQUEST['first_name'];
$last_name = $_REQUEST['last_name'];
$gender = $_REQUEST['gender'];
$address = $_REQUEST['address'];
$email = $_REQUEST['email'];
'$last_name','$gender','$address','$email')";
if(mysqli_query($conn, $sql)){
} else{
. mysqli_error($conn);
}
// Close connection
mysqli_close($conn);
?>
</center>
</body>
</html> Output :