Lab Programs - 6-10
Lab Programs - 6-10
Write a PHP script to reverse a given number and calculate its sum.
<html>
<head>
<title>Reverse Number Calculator</title>
</head>
<body>
<h2>PHP Script to Reverse a Given Number and Calculate its Sum</h2>
<form method="post">
Enter a Number:
<input type="text" name="num" id="number"/> <br> <br>
<input type="submit" name="submit" value="Check"/>
</form>
<?php
if($_POST){ // Check if the form is submitted
//get the value from form
$num = $_POST['num'];
//reversing the number
$reverse = strrev(strval($num));
</body>
</html>
Lab Program 7
Write a PHP script to generate a Fibonacci series using Recursive
function.
<?php
$num = 0;
$n1 = 0;
$n2 = 1;
echo "<h2> PHP Script to Generate a Fibonacci series for first 12 numbers
Using Recursive Function: </h2> \n";
echo $n1.' '.$n2.' ';
while ($num < 10 ) {
$n3 = $n2 + $n1;
echo $n3.' ';
$n1 = $n2;
$n2 = $n3;
$num = $num + 1;
}
?>
Lab Program 8
Write a PHP script to implement at least seven string functions.
<html>
<head>
<title>PHP String Functions</title>
</head>
<body>
<h2>PHP Script to Implement Seven String Functions</h2>
<form method="post">
Enter the main string: <input type="text" name="main_string"
required><br><br>
Enter secondary string: <input type="text" name="second_string"
required><br><br>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['main_string']) &&
isset($_POST['second_string'])) {
$main = $_POST['main_string'];
$second = $_POST['second_string'];
// 1. Convert to lowercase
echo "Lowercase: " . strtolower($main) . "<br>";
// 2. Convert to uppercase
echo "Uppercase: " . strtoupper($main) . "<br>";
</body>
</html>
Lab Program 9
Write a PHP program to insert new item in an array on any
position.
<!DOCTYPE html>
<html>
<head>
<title>Item Insertion Into Array</title>
</head>
<body>
<?php
// Define a fixed array
$original = array('1', '2', '3', '4', '5');
<hr>
<h2>PHP Program to Insert A New Item into an Array on any Position:\n </h2>
<form method="post">
Enter item to insert: <br>
<input type="text" name="item" required><br><br>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['item']) &&
isset($_POST['position'])) {
$item = $_POST['item'];
$position = intval($_POST['position']);
</body>
</html>
Lab Program 10
Write a PHP script to implement constructor and destructor.
<?php
echo "<h2> PHP Script to Implement Constructor and Destructor: </h2> \n";
class Student {
public $name;
// Constructor
public function __construct($studentName) {
$this->name = $studentName;
echo "Constructor is called. Welcome, $this->name!<br>";
}
// A simple method
public function introduce() {
echo "Hi, I am $this->name.<br>";
}
// Destructor
public function __destruct() {
echo "Destructor is called. Goodbye, $this->name!";
}
}
// Creating an object
$student1 = new Student("Hani");
// Call method
$student1->introduce();
?>