PHP Lab Manual
PHP Lab Manual
LAB MANUAL
With Answers
VI SEMESTER
As per the NEW Syllabus
For Students of VI Semester B.C.A Bangalore North University
Ms. Uma P
Assistant Professor, Department of Computer Science
New Horizon College, Kasturinagar, Bangalore.
Sl No Part A
1 Write a PHP script to Print “Hello world”.
2 Write a PHP script to find odd or even numbers from a given number.
3 Write a PHP script to find the maximum of three number
4 Write a PHP script to swap two numbers.
5 Write a PHP script to find the factorial of a number
Write a PHP script to check whether a given number is palindrome or
6
not
7 Write a PHP script to reverse a given number and calculate its sum
Write a PHP script to generate a Fibonacci series using a recursive
8
function
9 Write a PHP script to implement at least seven string functions.
Write a PHP script to insert a new item in an array on any position in
10
PHP
11 Write a PHP script to implement the constructor and destructor.
PHP & MySQL LAB
Part A
<?php
echo "<h1>Welcome</h1>"; // Outputs a page heading
print "<p style='font-weight:bold; color:blue;'>Hello world</p>"; // Outputs a styled
paragraph
?>
Output:
Welcome
Hello world
2. Write a PHP script to find odd or even numbers from a given number
<!DOCTYPE html>
<html>
<head>
<title>Odd or Even Number Checker</title>
</head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the input number from the form
$inputNumber = $_POST["number"];
<?php
// Function to swap two numbers
function swapNumbers(&$num1, &$num2) {
$temp = $num1;
$num1 = $num2;
$num2 = $temp;
}
// Example usage
$number1 = 5;
$number2 = 10;
<h2>Factorial Calculator</h2>
<?php
function factorial($n) {
if ($n === 0) {
return 1;
} else {
return $n * factorial($n - 1);
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the input number from the form
$inputNumber = $_POST["number"];
</body>
</html>
Output:
<h2>Palindrome Checker</h2>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the input number from the form
$inputNumber = $_POST["number"];
</body>
</html>
Output:
7. Write a PHP script to reverse a given number and calculate its sum
<!DOCTYPE html>
<html>
<head>
<title>Number Reversal and Digit Sum</title>
</head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the input number from the form
$inputNumber = $_POST["number"];
</body>
</html>
Output:
8. Write a PHP script to generate a Fibonacci series using a recursive function
<!DOCTYPE html>
<html>
<head>
<title>Fibonacci Series</title>
</head>
<body>
<h2>Fibonacci Series</h2>
<?php
function fibonacci($n) {
if ($n == 0) {
return 0;
} elseif ($n == 1) {
return 1;
} else {
return (fibonacci($n - 1) + fibonacci($n - 2));
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the number of terms from the form
$numTerms = $_POST["terms"];
<?php
// Sample string
$string = "Hello, World!";
// 1. String Length
echo "<p>1. String Length: " . strlen($string) . "</p>";
// 2. Convert to Uppercase
echo "<p>2. Convert to Uppercase: " . strtoupper($string) . "</p>";
// 3. Convert to Lowercase
echo "<p>3. Convert to Lowercase: " . strtolower($string) . "</p>";
// 4. Substring
$start = 0;
$length = 5;
echo "<p>4. Substring (first 5 characters): " . substr($string, $start, $length) . "</p>";
// 5. String Replace
$old = "World";
$new = "PHP";
$newString = str_replace($old, $new, $string);
echo "<p>5. String Replace: " . $newString . "</p>";
// 6. String Position (strpos)
$search = "World";
$position = strpos($string, $search);
echo "<p>6. String Position (strpos): $search found at position $position</p>";
// 7. Trim Whitespace
$whitespaceString = " Trim Me ";
$trimmedString = trim($whitespaceString);
echo "<p>7. Trim Whitespace: '$whitespaceString' trimmed to '$trimmedString'</p>";
?>
</body>
</html>
Output:
10. Write a PHP script to insert a new item in an array on any position in PHP
<?php
// Sample array
$originalArray = array("apple", "banana", "cherry", "date");
print_r($originalArray);
// Item to insert
$newItem = "orange";
// Define a class
class MyClass {
// Properties
public $name;
// Constructor
public function __construct($name) {
echo "Constructing {$name}\n";
$this->name = $name;
}
// Destructor
public function __destruct() {
echo "Destructing {$this->name}\n";
}
// Method
public function greet() {
echo "Hello, {$this->name}!\n";
}
}
// Create an object
$obj1 = new MyClass("Object 1");
$obj1->greet();
?>
Output: Constructing Object 1 Hello, Object 1! Constructing Object 2 Hello, Object
2! Destructing Object 1 Destructing Object 2