[go: up one dir, main page]

0% found this document useful (0 votes)
5 views7 pages

Lab Programs - 6-10

The document contains several PHP lab programs that demonstrate various programming concepts. These include reversing a number and calculating its sum, generating a Fibonacci series using recursion, implementing string functions, inserting an item into an array, and creating a class with a constructor and destructor. Each program is presented with HTML forms for user input and PHP code for processing the input.

Uploaded by

suraiyareeha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Lab Programs - 6-10

The document contains several PHP lab programs that demonstrate various programming concepts. These include reversing a number and calculating its sum, generating a Fibonacci series using recursion, implementing string functions, inserting an item into an array, and creating a class with a constructor and destructor. Each program is presented with HTML forms for user input and PHP code for processing the input.

Uploaded by

suraiyareeha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab Program 6

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));

// Calculate the sum of the digits in the reversed number


$sum = 0;
for ($i = 0; $i < strlen($reverse); $i++) {
$sum += intval($reverse[$i]);
}

echo "Reversed number: " . $reverse . "<br>";


echo "Sum of digits: " . $sum . "<br>";
}
?>

</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'];

echo "<h3>Original Strings:</h3>";


echo "Main String: $main <br>";
echo "Secondary String: $second <br><br>";

// 1. Convert to lowercase
echo "Lowercase: " . strtolower($main) . "<br>";

// 2. Convert to uppercase
echo "Uppercase: " . strtoupper($main) . "<br>";

// 3. Find position of secondary string in main string


$pos = strpos($main, $second);
echo "Position of '$second' in main string: " . ($pos !== false ? $pos :
"Not found") . "<br>";

// 4. Replace secondary string in main string with 'Replaced'


echo "String after replacing '$second' with 'Replaced': " .
str_replace($second, "Replaced", $main) . "<br>";

// 5. Case-sensitive string comparison


echo "Case-sensitive comparison: " . strcmp($main, $second) . "<br>";
// 6. Reverse the main string
echo "Reversed main string: " . strrev($main) . "<br>";

// 7. Length of main string


echo "Length of main string: " . strlen($main) . "<br>";

// 8. Count words in main string


echo "Number of words in main string: " . str_word_count($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');

// Display the original array initially


echo "<h2>Original Array:</h2>";
echo implode(" ", $original);
?>

<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>

Enter position to insert at (0-based index): <br>


<input type="number" name="position" min="0" required><br><br>

<input type="submit" value="Insert Item">


</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['item']) &&
isset($_POST['position'])) {
$item = $_POST['item'];
$position = intval($_POST['position']);

// Copy the original array to avoid modifying the top display


$modified_array = $original;

if ($position >= 0 && $position <= count($modified_array)) {


// Insert the new item
array_splice($modified_array, $position, 0, $item);
// Show result
echo "<h3>Modified Array after inserting '$item' at position
$position:</h3>";
echo implode(" ", $modified_array);
} else {
echo "<p style='color:red;'>Invalid position! Must be between 0 and "
. count($modified_array) . ".</p>";
}
}
?>

</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();

?>

You might also like