PHP Lab Manual-2-2
PHP Lab Manual-2-2
Rectangle
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Area Calculator</title>
</head>
<body>
<h2>Area Calculator</h2>
<form method="post">
<label for="base">Base:</label>
<input type="number" id="base" name="base" required><br><br>
<label for="height">Height:</label>
<input type="number" id="height" name="height" required><br><br>
<?php
if (isset($_POST['calculate'])) {
$base = $_POST['base'];
$height = $_POST['height'];
$areaRectangle = $base * $height;
$areaTriangle = ($base * $height) / 2;
echo "<br><strong>Results:</strong><br>";
echo "The area of a rectangle with base $base and height $height is $areaRectangle
square units.<br>";
echo "The area of a triangle with base $base and height $height is $areaTriangle
square units.";
}
?>
</body>
</html>
Output
1b) Develop a PHP program to calculate compound interest .
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Compound Interest Calculator</title>
</head>
<body>
<h2>Compound Interest Calculator</h2>
<form method="post">
<label for="principal">Principal Amount:</label>
<input type="number" id="principal" name="principal" required><br><br>
<label for="rate">Annual Interest Rate (%):</label>
<input type="number" id="rate" name="rate" step="0.01" required><br><br>
<label for="time">Number of Years:</label>
<input type="number" id="time" name="time" required><br><br>
<input type="submit" name="calculate" value="Calculate">
</form>
<?php
if (isset($_POST['calculate'])) {
$principal = $_POST['principal'];
$rate = $_POST['rate'] / 100; // Convert percentage to decimal
$time = $_POST['time'];
$compound_per_year = 1;
// Calculate compound interest
$compound_interest = $principal * (1 + ($rate / $compound_per_year)) **
($compound_per_year * $time) - $principal;
echo "<br><strong>Results:</strong><br>";
echo "Principal Amount: $principal<br>";
echo "Annual Interest Rate: $rate<br>";
echo "Number of Years: $time<br>";
echo "Compound Interest: $compound_interest";
}
?>
</body>
</html>
Output
2. Demonstrating the various forms to concatenate multiple strings Develop
program(s) to demonstrate concatenation of strings:
(i) Strings represented with literals (single quote or double quote).
(ii) Strings as variables.
(iii) Multiple strings represented with literals (single quote or double quote) and
variables.
(iv) Strings and string variables containing single quotes as part string contents.
(v) Strings containing HTML segments having elements with attributes.
<?php
// Concatenation using literals
$string1 = 'Hello, ';
$string2 = "world!";
$result = $string1 . $string2;
echo $result;
?>
</body>
</html>
(ii)Strings as variables.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>String Concatenation</title>
</head>
<body>
<?php
// Concatenation using variables
$name = "John";
$greeting = "Hello, " . $name . "!";
echo $greeting;
?>
</body>
</html>
Output: Hello, John!
(iii)Multiple strings represented with literals (single quote or double quote) and
variables.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>String Concatenation</title>
</head>
<body>
<?php
// Concatenation using literals and variables
$firstName = 'John';
$lastName = "Doe";
$greeting1 = "Hello, " . $firstName . ' ' . $lastName . "!";
echo $greeting1;
?>
</body>
</html>
Output: Hello, John Doe!
(iv)Strings and string variables containing single quotes as part string contents.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>String Concatenation</title>
</head>
<body>
<?php
// Concatenation with strings containing single quotes
$singleQuotedString = 'It\'s a sunny day.';
$variableWithSingleQuotes = "I said, '$singleQuotedString'";
echo $variableWithSingleQuotes;
?>
</body>
</html>
Output: I said, 'It's a sunny day.'
(v)Strings containing HTML segments having elements with attributes.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>String Concatenation</title>
</head>
<body>
<?php
// Concatenation with HTML segments
$element = 'div';
$className = 'my-class';
$content = 'This is PHP LAB.';
$htmlString = '<' . $element . ' class="' . $className . '">' . $content . '</' . $element .
'>';
echo $htmlString;
?>
</body>
</html>
Output: This is PHP LAB.
<!DOCTYPE html>
<html>
<head>
<title>Number Checker</title>
</head>
<body>
<h2>Number Checker</h2>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get user input
$givenNumber = $_POST["number"];
$divisor = $_POST["divisor"];
$baseNumber = $_POST["baseNumber"];
</body>
</html>
Output:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get user input
$a = $_POST["a"];
$b = $_POST["b"];
$c = $_POST["c"];
if ($discriminant > 0) {
$root1 = (-$b + sqrt($discriminant)) / (2 * $a);
$root2 = (-$b - sqrt($discriminant)) / (2 * $a);
return "Root 1: $root1, Root 2: $root2";
} elseif ($discriminant == 0) {
$root = -$b / (2 * $a);
return "Root: $root (double root)";
} else {
return "No real roots (complex roots)";
}
}
Output:
4a. Develop a PHP program to find the square root of a number by using the
newton’s algorithm.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Square Root Calculator</title>
</head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = isset($_POST["number"]) ? $_POST["number"] : null;
if (!is_numeric($number)) {
echo "<p>Please enter a valid number.</p>";
} else {
$result = squareRoot($number);
echo "<p>The square root of $number is approximately $result.</p>";
}
}
function squareRoot($number) {
// Newton's algorithm for square root
$guess = $number / 2;
$epsilon = 1e-10; // small value for precision
return $guess;
}
?>
</body>
</html>
Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Floyd's Triangle Generator</title>
</head>
<body>
<h2>Floyd's Triangle</h2>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$rows = isset($_POST["rows"]) ? $_POST["rows"] : null;
function generateFloydsTriangle($rows) {
$number = 1;
</body>
</html>
Output
5a. Develop a PHP application that reads a list of numbers and calculates mean
and standard deviation.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mean and Standard Deviation Calculator</title>
</head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$numbersInput = isset($_POST["numbers"]) ? $_POST["numbers"] : null;
if (empty($numbersInput)) {
echo "<p>Please enter a list of numbers.</p>";
} else {
$numbers = array_map('floatval', explode(',', $numbersInput));
if (count($numbers) < 2) {
echo "<p>Please enter at least two numbers for calculation.</p>";
} else {
$mean = calculateMean($numbers);
$stdDev = calculateStandardDeviation($numbers);
function calculateMean($numbers) {
$sum = array_sum($numbers);
$count = count($numbers);
return $count > 0 ? $sum / $count : 0;
}
function calculateStandardDeviation($numbers) {
$mean = calculateMean($numbers);
$variance = 0;
$variance /= count($numbers);
return sqrt($variance);
}
?>
</body>
</html>
Output
5b. Develop a PHP application that reads scores between 0 and 100 (possibly
including both 0 and 100) and creates a histogram array whose elements contain
the number of scores between 0 and 9, 10 and 19, etc. The last “box” in the
histogram should include scores between 90 and 100. Use a function to generate
the histogram.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Score Histogram Generator</title>
</head>
<body>
if (empty($scoresInput)) {
echo "<p>Please enter scores.</p>";
} else {
$scores = array_map('intval', explode(',', $scoresInput));
if (validateScores($scores)) {
$histogram = generateHistogram($scores);
function validateScores($scores) {
foreach ($scores as $score) {
if (!is_numeric($score) || $score < 0 || $score > 100) {
return false;
}
}
return true;
}
function generateHistogram($scores) {
$histogram = array_fill(0, 11, 0); // Initialize histogram array with 11 elements
return $histogram;
}
?>
</body>
</html>
This HTML file includes a form with an input field to enter scores (comma-separated)
between 0 and 100. When the form is submitted, the PHP code checks if the input is
valid, generates a histogram based on the specified ranges, and displays the results on
the page. The validateScores function ensures that the entered scores are valid, and
the generateHistogram function creates the histogram array.
Output
6a. Develop PHP program to demonstrate the date() with different parameter
options.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Date() Function Demo</title>
</head>
<body>
<h1>Date() Function Demo</h1>
<p>Current Date and Time (Indian Standard Time):</p>
<?php
// Set timezone to Indian Standard Time
date_default_timezone_set('Asia/Kolkata');
?>
<p><?php echo date('Y-m-d H:i:s'); ?></p>
<h2>Formatting Options:</h2>
<ul>
<li>Year-Month-Day: <?php echo date('Y-m-d'); ?></li>
<li>Month-Day-Year: <?php echo date('m-d-Y'); ?></li>
<li>Day-Month-Year: <?php echo date('d-m-Y'); ?></li>
<li>Hour:Minute:Second: <?php echo date('H:i:s'); ?></li>
<li>Day of the Week: <?php echo date('l'); ?></li>
<li>Month Name: <?php echo date('F'); ?></li>
</ul>
</body>
</html>
Output:
6.b Develop a PHP program to generate the Fibonacci series using a recursive
function.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fibonacci Series</title>
</head>
<body>
<h1>Fibonacci Series Generator</h1>
<form method="post">
<label for="terms">Enter the number of terms:</label>
<input type="number" id="terms" name="terms">
<button type="submit">Generate Fibonacci Series</button>
</form>
<?php
// Recursive function to generate Fibonacci series
function fibonacci($n) {
if ($n <= 1) {
return $n;
} else {
return (fibonacci($n - 1) + fibonacci($n - 2));
}
}
Output:
9 Develop a PHP program to filter the elements of an array with key names.
Sample Input Data:
1st array: ('c1' => 'Red', 'c2' => 'Green', 'c3' => 'White', c4 => 'Black')
2nd array: ('c2', 'c4')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Array Key Filter</title>
</head>
<body>
<h1>Array Key Filter</h1>
<?php
// Check if form is submitted
if (isset($_POST['submit'])) {
// Get input arrays from form
$array1_input = $_POST['array1'];
$array2_input = $_POST['array2'];
Output:
10 . Develop a PHP program that illustrates the concept of classes and objects
by reading and printing employee data, including Emp_Name, Emp_ID,
Emp_Dept, Emp_Salary, and Emp_DOJ.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Employee Data</title>
</head>
<body>
<h1>Employee Data</h1>
<?php
// Define Employee class
class Employee {
public $name;
public $id;
public $dept;
public $salary;
public $doj;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Aadhaar Number Occurrence Counter</title>
</head>
<body>
<h1>Aadhaar Number Occurrence Counter</h1>
<?php
// Function to count occurrences of Aadhaar numbers in text
function countAadhaarOccurrences($text) {
// Regular expression to match Aadhaar numbers
$pattern = '/\b[0-9]{4}\s[0-9]{4}\s[0-9]{4}\b/';
Output:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pattern Occurrence Replacer</title>
</head>
<body>
<h1>Pattern Occurrence Replacer</h1>
<?php
// Function to find occurrences of a pattern and replace them with a text
function replaceOccurrences($text, $pattern, $replacement) {
// Perform the replacement
$result = preg_replace($pattern, $replacement, $text, -1, $count);
}
?>
</body>
</html>
Output:
12.Develop a PHP program to read the contents of a HTML form and display
the contents on a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Content Display</title>
</head>
<body>
<h1>Form Content Display</h1>
<?php
// Check if form is submitted
if (isset($_POST['submit'])) {
// Retrieve input data from form
$name = $_POST['name'];
$email = $_POST['email'];
Output: