PHP & MySQL
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
1. Write a PHP script to Print “Hello world”.
<?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>
<h2>Odd or Even Number Checker</h2>
<form method="post" action="">
Enter a number: <input type="number" name="number">
<input type="submit" value="Check">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the input number from the form
$inputNumber = $_POST["number"];
// Check if the number is odd or even
if ($inputNumber % 2 == 0) {
echo "<p>{$inputNumber} is an even number.</p>";
} else {
echo "<p>{$inputNumber} is an odd number.</p>";
}
}
?>
</body>
</html>
Output:
3. Write a PHP script to find the maximum of three number
<?php
// Define three numbers
$num1 = 25;
$num2 = 48;
$num3 = 36;
// Find the maximum number
$max = $num1;
if ($num2 > $max) {
$max = $num2;
}
if ($num3 > $max) {
$max = $num3;
}
// Display the maximum number
echo "The maximum of $num1, $num2, and $num3 is: $max";
?>
Output:
The maximum of 25, 48, and 36 is: 48
4. Write a PHP script to swap two numbers.
<?php
// Function to swap two numbers
function swapNumbers(&$num1, &$num2) {
$temp = $num1;
$num1 = $num2;
$num2 = $temp;
}
// Example usage
$number1 = 5;
$number2 = 10;
echo "Before swapping: ";
echo "Number 1: " . $number1 . ", Number 2: " . $number2 . "<br>";
// Call the function to swap numbers
swapNumbers($number1, $number2);
echo "After swapping: ";
echo "Number 1: " . $number1 . ", Number 2: " . $number2 . "<br>";
?>
Output:
Before swapping: Number 1: 5, Number 2: 10
After swapping: Number 1: 10, Number 2: 5
5. Write a PHP script to find the factorial of a number
<!DOCTYPE html>
<html>
<head>
<title>Factorial Calculator</title>
</head>
<body>
<h2>Factorial Calculator</h2>
<form method="post" action="">
Enter a number: <input type="number" name="number" required>
<input type="submit" value="Calculate Factorial">
</form>
<?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"];
// Calculate the factorial
$result = factorial($inputNumber);
// Display the factorial
echo "<p>The factorial of $inputNumber is: $result</p>";
}
?>
</body>
</html>
Output:
6. Write a PHP script to check whether a given number is palindrome or not
<!DOCTYPE html>
<html>
<head>
<title>Palindrome Checker</title>
</head>
<body>
<h2>Palindrome Checker</h2>
<form method="post" action="">
Enter a number: <input type="number" name="number" required>
<input type="submit" value="Check Palindrome">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the input number from the form
$inputNumber = $_POST["number"];
// Reverse the number
$reverseNumber = strrev($inputNumber);
// Check if the original and reversed numbers are the same
if ($inputNumber == $reverseNumber) {
echo "<p>{$inputNumber} is a palindrome number.</p>";
} else {
echo "<p>{$inputNumber} is not a palindrome number.</p>";
}
}
?>
</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>
<h2>Number Reversal and Digit Sum</h2>
<form method="post" action="">
Enter a number: <input type="number" name="number" required>
<input type="submit" value="Reverse and Calculate Sum">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the input number from the form
$inputNumber = $_POST["number"];
// Reverse the number
$reversedNumber = strrev($inputNumber);
// Calculate the sum of the digits
$sum = 0;
$length = strlen($reversedNumber);
for ($i = 0; $i < $length; $i++) {
$sum += (int)$reversedNumber[$i];
}
// Display the reversed number and the sum of its digits
echo "<p>Reversed Number: $reversedNumber</p>";
echo "<p>Sum of Digits: $sum</p>";
}
?>
</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>
<form method="post" action="">
Enter the number of terms: <input type="number" name="terms" required>
<input type="submit" value="Generate Fibonacci Series">
</form>
<?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"];
// Generate and display the Fibonacci series
echo "<p>Fibonacci Series with $numTerms terms:</p>";
for ($i = 0; $i < $numTerms; $i++) {
echo fibonacci($i) . " ";
}
}
?>
</body>
</html>
Output:
9. Write a PHP script to implement at least seven string functions.
<!DOCTYPE html>
<html>
<head>
<title>String Functions Demo</title>
</head>
<body>
<h2>String Functions Demo</h2>
<?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:
String Functions Demo
1. String Length: 13
2. Convert to Uppercase: HELLO, WORLD!
3. Convert to Lowercase: hello, world!
4. Substring (first 5 characters): Hello
5. String Replace: Hello, PHP!
6. String Position (strpos): World found at position 7
7. Trim Whitespace: ' Trim Me ' trimmed to 'Trim Me'
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";
// Position to insert (0-based index)
$positionToInsert = 2;
// Insert the new item at the specified position
array_splice($originalArray, $positionToInsert, 0, $newItem);
// Display the updated array
echo "Updated Array: ";
print_r($originalArray);
?>
Output:
Array ( [0] => apple [1] => banana [2] => cherry [3] => date ) Updated Array:
Array ( [0] => apple [1] => banana [2] => orange [3] => cherry [4] => date )
11. Write a PHP script to implement the constructor and destructor
<?php
// 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();
// Create another object
$obj2 = new MyClass("Object 2");
$obj2->greet();
// Unset the objects
unset($obj1);
unset($obj2);
?>
Output: Constructing Object 1 Hello, Object 1! Constructing Object 2 Hello, Object
2! Destructing Object 1 Destructing Object 2