0% found this document useful (0 votes)
10 views

php slip solution

The document contains various HTML and PHP code snippets demonstrating different functionalities, including displaying a list of Indian states and capitals, sorting arrays, file handling, creating navigation bars, and performing mathematical operations. It also includes examples of HTML forms for user input and styling using CSS. Additionally, there are examples of temperature reporting and string manipulation functions in PHP.

Uploaded by

mydesktop2690
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

php slip solution

The document contains various HTML and PHP code snippets demonstrating different functionalities, including displaying a list of Indian states and capitals, sorting arrays, file handling, creating navigation bars, and performing mathematical operations. It also includes examples of HTML forms for user input and styling using CSS. Additionally, there are examples of temperature reporting and string manipulation functions in PHP.

Uploaded by

mydesktop2690
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

*Slip 1

Q.1.
<!DOCTYPE html>
<html>
<head>
<title>Indian States and Capitals</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 40px;
}
h1 {
font-size: 24px;
margin-bottom: 20px;
}
ol {
font-size: 18px;
}
li {
margin-bottom: 10px;
}
.capital {
margin-left: 20px;
list-style-type: circle;
}
</style>
</head>
<body>
<h1>List of Indian States with their Capital</h1>
<ol>
<li>Delhi
<ul class="capital">
<li>New Delhi</li>
</ul>
</li>
<li>Haryana
<ul class="capital">
<li>Chandigarh</li>
</ul>
</li>
<li>Gujarat
<ul class="capital">
<li>Gandhinagar</li>
</ul>
</li>
<li>Rajasthan
<ul class="capital">
<li>Jaipur</li>
</ul>
</li>
<li>Maharashtra
<ul class="capital">
<li>Mumbai</li>
</ul>
</li>
<li>Uttar Pradesh
<ul class="capital">
<li>Lucknow</li>
</ul>
</li>
</ol>
</body>
</html>

Q.2.
<?php
// Original array
$people = array("Sophia"=>"31", "Jacob"=>"41", "William"=>"39", "Ramesh"=>"40");

// a) Ascending order sort by value


asort($people);
echo "a) Ascending order sort by value:<br>";
foreach($people as $key => $value){
echo "$key => $value<br>";
}

echo "<br>";

// b) Ascending order sort by key


ksort($people);
echo "b) Ascending order sort by key:<br>";
foreach($people as $key => $value){
echo "$key => $value<br>";
}

echo "<br>";

// c) Descending order sort by value


arsort($people);
echo "c) Descending order sort by value:<br>";
foreach($people as $key => $value){
echo "$key => $value<br>";
}

echo "<br>";

// d) Descending order sort by key


krsort($people);
echo "d) Descending order sort by key:<br>";
foreach($people as $key => $value){
echo "$key => $value<br>";
}
?>
*Slip 2
Q.1.
<?php
// File path
$filename = "sample.txt"; // You can change this to your desired file

// Check if the file exists


if (file_exists($filename)) {
// Read and display the content
$contents = file_get_contents($filename);
echo "<h3>Contents of the file:</h3>";
echo "<pre>$contents</pre>";

// Get and display file size


$size = filesize($filename);
echo "<h3>File Size:</h3>";
echo "$size bytes";
} else {
echo "File '$filename' does not exist.";
}
?>

Q.2.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Navigation Bar</title>
<style>
body {
margin: 0;
padding: 0;
}
ul.navbar {
list-style-type: none;
margin: 0;
padding: 0;
background-color: lightgray;
overflow: hidden;
}
ul.navbar li {
float: left;
}
ul.navbar li a {
display: block;
color: blue;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 18px;
}
ul.navbar li:first-child a {
background-color: #555;
color: white;
font-weight: bold;
}
</style>
</head>
<body>

<ul class="navbar">
<li><a href="#">Home</a></li>
<li><a href="#">Java</a></li>
<li><a href="#">HTML</a></li>
<li><a href="#">CSS</a></li>
</ul>

</body>
</html>
*Slip 3
Q.1.
HTML Form (input.html or inside the same PHP file)

<!DOCTYPE html>
<html>
<head>
<title>Math Operations</title>
</head>
<body>
<h2>Enter Two Integers</h2>
<form action="operations.php" method="post">
Enter First Number: <input type="text" name="num1"><br><br>
Enter Second Number: <input type="text" name="num2"><br><br>
<input type="submit" value="Calculate">
</form>
</body>
</html>
PHP Script (operations.php)

<?php
// Function to find modulus
function findMod($a, $b) {
return $a % $b;
}

// Function to find power


function findPower($a, $b) {
return pow($a, $b);
}

// Function to find sum of first n numbers


function sumFirstN($n) {
return ($n * ($n + 1)) / 2;
}

// Function to find factorial


function factorial($n) {
if ($n == 0 || $n == 1) {
return 1;
}
$fact = 1;
for ($i = 2; $i <= $n; $i++) {
$fact *= $i;
}
return $fact;
}

// Check if form is submitted


if ($_SERVER["REQUEST_METHOD"] == "POST") {
$num1 = (int)$_POST['num1'];
$num2 = (int)$_POST['num2'];

echo "<h3>Results:</h3>";
echo "a. Modulus of $num1 % $num2 = " . findMod($num1, $num2) . "<br>";
echo "b. Power ($num1 ^ $num2) = " . findPower($num1, $num2) . "<br>";
echo "c. Sum of first $num1 numbers = " . sumFirstN($num1) . "<br>";
echo "d. Factorial of $num2 = " . factorial($num2) . "<br>";
}
?>
Q.2.
HTML File (plants.html)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Plant Types</title>
<link rel="stylesheet" href="style.css">
</head>
<body>

<ul class="main-list">
<li class="non-flowering">Non flowering plants
<ul class="circle-list">
<li class="fern">Fern</li>
<li class="spore">Spore</li>
</ul>
</li>
<li class="flowering">Flowering plants
<ul class="square-list">
<li class="lilly">Lilly</li>
<li class="rose">Rose
<ol class="numbered-list">
<li class="red-rose">Red Rose</li>
<li class="pink-rose">Pink Rose</li>
</ol>
</li>
</ul>
</li>
</ul>

</body>
</html>
External CSS File (style.css)

body {
font-family: Arial, sans-serif;
background-color: #f9f9f9;
}
.main-list {
font-size: 20px;
color: #000;
}
.non-flowering {
font-size: 22px;
font-family: 'Georgia', serif;
color: darkgreen;
}

.flowering {
font-size: 22px;
font-family: 'Verdana', sans-serif;
color: darkred;
}
.circle-list {
list-style-type: circle;
font-size: 18px;
}
.fern {
font-family: 'Courier New', monospace;
color: #007BFF;
}
.spore {
font-family: 'Tahoma', sans-serif;
color: #8B4513;
}
.square-list {
list-style-type: square;
font-size: 18px;
}
.lilly {
font-family: 'Comic Sans MS', cursive;
color: purple;
}
.rose {
font-family: 'Times New Roman', serif;
color: crimson;
}
.numbered-list {
font-size: 16px;
list-style-type: decimal;
}
.red-rose {
font-family: 'Trebuchet MS', sans-serif;
color: red;
}
.pink-rose {
font-family: 'Lucida Console', monospace;
color: deeppink;
}
*Slip 4
Q.1.
index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS Box Model Property</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>CSS Box Model Property</h1>

<div class="outer-box">
<div class="inner-box">
<div class="content-box">T.Y.B.Sc(Comp.Sci)</div>
<div class="content-box">Academic Year 2021-22</div>
</div>
</div>
</body>
</html>
style.css

body {
text-align: center;
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
h1 {
margin-top: 20px;
font-size: 28px;
}
.outer-box {
margin: 20px auto;
width: 600px;
height: 400px;
background-color: gold;
border: 5px solid black;
padding: 50px;
box-sizing: border-box;
}
.inner-box {
background-color: maroon;
padding: 40px;
border: 3px solid black;
box-sizing: border-box;
}
.content-box {
background-color: white;
margin: 20px auto;
padding: 15px;
border: 2px solid black;
width: 60%;
font-size: 18px;
}
Q.2.
<?php
// Define an array
$fruits = array("apple", "banana", "orange", "apple", "mango", "banana", "apple");
// Define the value to search
$search_value = "apple";
// Use array_keys() to find all keys with matching value
$matching_keys = array_keys($fruits, $search_value);
// Display results
if (!empty($matching_keys)) {
echo "Value '$search_value' found at the following positions:<br>";
foreach ($matching_keys as $key) {
echo "Index: $key, Value: " . $fruits[$key] . "<br>";
}
} else {
echo "Value '$search_value' not found in the array.";
}
?>
*slip 5
Q.1.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My City – Pune</title>
</head>
<body style="background-color: pink;">
<!-- City Name at Top -->
<h1 style="color: blue; text-align: center; font-size: 48px;">PUNE</h1>
<!-- Scrolling Text -->
<marquee style="color: darkred; font-size: 24px; font-family: Verdana;">
Welcome to Pune – The cultural capital of Maharashtra, known for its history, education,
and vibrant life!
</marquee>
<!-- Landmarks -->
<h2 style="color: darkgreen; font-family: Georgia;">Shaniwar Wada</h2>
<h2 style="color: orange; font-family: 'Courier New', monospace;">Aga Khan Palace</h2>
<h2 style="color: purple; font-family: Arial;">Sinhagad Fort</h2>
<h2 style="color: navy; font-family: Tahoma;">Dagdusheth Ganpati</h2>
<!-- Image at the Bottom -->
<div style="text-align: center; margin-top: 50px;">
<img
src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f0/Shaniwarwada_Pune.jpg/8
00px-Shaniwarwada_Pune.jpg"
alt="Shaniwar Wada"
style="width: 500px; height: auto; border: 5px solid black;">
</div>
</body>
</html>

Q.2.
<?php
// Define array of 30 high temperatures for a spring month (in Celsius)
$high_temps = array(24, 26, 22, 25, 27, 29, 28, 24, 23, 26, 30, 31, 25, 24, 26, 27, 28, 29, 30, 28,
27, 26, 25, 24, 23, 22, 21, 25, 26, 27);
// Calculate average
$average = array_sum($high_temps) / count($high_temps);
// Sort to get coolest and warmest temperatures
sort($high_temps); // Ascending order for coolest
$coolest = array_slice($high_temps, 0, 5);
rsort($high_temps); // Descending order for warmest
$warmest = array_slice($high_temps, 0, 5);
// Display results
echo "<h2>Spring Month Temperature Report</h2>";
echo "<p><strong>Average High Temperature:</strong> " . round($average, 2) . "&deg;C</p>";
echo "<p><strong>Five Coolest High Temperatures:</strong> ";
foreach ($coolest as $temp) {
echo $temp . "&deg;C, ";
}
echo "</p>";
echo "<p><strong>Five Warmest High Temperatures:</strong> ";
foreach ($warmest as $temp) {
echo $temp . "&deg;C, ";
}
echo "</p>";
?>
*slip 6
Q.1.
<!DOCTYPE html>
<html>
<head>
<title>Operating System Information</title>
<style>
body {
background-color: yellow;
font-family: Arial, sans-serif;
}
h2 {
font-size: 6pt;
color: red;
}
</style>
</head>
<body>
<h2>Operating System Information</h2>
<form>
<label>Enter your name</label>
<input type="text" name="name"><br><br>
<label>Password</label>
<input type="password" name="password"><br><br>

<label>Which of the following Operating System have you used</label><br>


<input type="checkbox" name="used[]" value="Linux" checked> Linux
<input type="checkbox" name="used[]" value="Windows 10" checked> Windows 10
<input type="checkbox" name="used[]" value="Macintosh 8.0"> Macintosh 8.0
<br><br>
<label>Which of the Operating System do you like the best?</label><br>
<input type="radio" name="best" value="Linux" checked> Linux
<input type="radio" name="best" value="Windows 10"> Windows 10
<input type="radio" name="best" value="Macintosh 8.0"> Macintosh 8.0
<br><br>
<label>You have completed the form</label>
<input type="submit" value="Sign Up">
</form>
</body>
</html>

Q.2.
string_functions.php (Create this file separately)

<?php
function getLength($str) {
$length = 0;
while (isset($str[$length])) {
$length++;
}
return $length;
}
function countVowels($str) {
$count = 0;
$vowels = ['a', 'e', 'i', 'o', 'u'];
$str = strtolower($str);
for ($i = 0; $i < strlen($str); $i++) {
if (in_array($str[$i], $vowels)) {
$count++;
}
}
return $count;
}
function toTitleCase($str) {
return ucwords(strtolower($str));
}
function padString($str) {
return str_pad($str, strlen($str)+4, "*", STR_PAD_BOTH);
}
function removeLeadingWhitespace($str) {
return ltrim($str);
}
function reverseString($str) {
return strrev($str);
}
?>
string_operations.php (Main file with form and logic)

<!DOCTYPE html>
<html>
<head>
<title>String Operations</title>
</head>
<body>
<h2>String Operation Form</h2>
<form method="post">
Enter a string: <input type="text" name="inputStr"><br><br>
<input type="radio" name="operation" value="length">Find Length<br>
<input type="radio" name="operation" value="vowels">Count Vowels<br>
<input type="radio" name="operation" value="titlecase">Convert to Title Case<br>
<input type="radio" name="operation" value="pad">Pad with *<br>
<input type="radio" name="operation" value="trim">Remove Leading Whitespaces<br>
<input type="radio" name="operation" value="reverse">Reverse String<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
include("string_functions.php");
if (isset($_POST['submit'])) {
$str = $_POST['inputStr'];
$operation = $_POST['operation'];
switch ($operation) {
case "length":
echo "<p>Length: " . getLength($str) . "</p>";
break;
case "vowels":
echo "<p>Vowel Count: " . countVowels($str) . "</p>";
break;
case "titlecase":
echo "<p>Title Case: " . toTitleCase($str) . "</p>";
break;
case "pad":
echo "<p>Padded String: " . padString($str) . "</p>";
break;
case "trim":
echo "<p>Without Leading Whitespaces: '" . removeLeadingWhitespace($str) . "'</p>";
break;
case "reverse":
echo "<p>Reversed String: " . reverseString($str) . "</p>";
break;
default:
echo "<p>Please select an operation.</p>";
}
}
?>
</body>
</html>
*slip 7
Q.1.
<!DOCTYPE html>
<html>
<head>
<title>Greeting Generator</title>
</head>
<body>
<h2>Greeting Form</h2>
<form method="post">
Student Name: <input type="text" name="student"><br><br>
College Name: <input type="text" name="college"><br><br>
Greeting Message: <input type="text" name="greeting"><br><br>
<input type="submit" name="submit" value="Generate Greeting">
</form>
<?php
function generateGreeting($student = "Student", $college = "your college", $greeting =
"Welcome!") {
echo "<p>$greeting $student from $college.</p>";
}
if (isset($_POST['submit'])) {
$student = !empty($_POST['student']) ? $_POST['student'] : null;
$college = !empty($_POST['college']) ? $_POST['college'] : null;
$greeting = !empty($_POST['greeting']) ? $_POST['greeting'] : null;
generateGreeting($student, $college, $greeting);
}
?>
</body>
</html>

Q.2.
HTML (save as plants.html)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Plant Types</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<ul>
<li class="nonflowering">Non flowering plants
<ul>
<li class="fern">Fern</li>
<li class="spore">Spore</li>
</ul>
</li>
<li class="flowering">Flowering plants
<ul>
<li class="lilly">Lilly</li>
<li class="rose">Rose
<ol>
<li class="redrose">Red Rose</li>
<li class="pinkrose">Pink Rose</li>
</ol>
</li>
</ul>
</li>
</ul>
</body>
</html>
External CSS (save as style.css)

body {
font-family: Arial, sans-serif;
}
.nonflowering {
font-size: 20px;
color: brown;
font-family: 'Georgia', serif;
}
.fern {
font-size: 18px;
color: green;
font-family: 'Courier New', monospace;
}
.spore {
font-size: 18px;
color: darkgreen;
font-family: 'Verdana', sans-serif;
}
.flowering {
font-size: 20px;
color: purple;
font-family: 'Tahoma', sans-serif;
}
.lilly {
font-size: 18px;
color: blue;
font-family: 'Lucida Console', monospace;
}
.rose {
font-size: 18px;
color: maroon;
font-family: 'Arial', sans-serif;
}
.redrose {
font-size: 16px;
color: red;
font-family: 'Times New Roman', serif;
}
.pinkrose {
font-size: 16px;
color: deeppink;
font-family: 'Comic Sans MS', cursive;
}
*slip 8
Q.1.
<?php
$stack = array();
$queue = array();
function displayMenu() {
echo "\n\n--- MENU ---\n";
echo "1. Insert in Stack\n";
echo "2. Delete from Stack\n";
echo "3. Display Stack\n";
echo "4. Insert in Queue\n";
echo "5. Delete from Queue\n";
echo "6. Display Queue\n";
echo "7. Exit\n";
echo "Enter your choice: ";
}
// Loop to handle menu choices
do {
displayMenu();
$choice = (int)readline();
switch ($choice) {
case 1:
$element = readline("Enter element to insert in stack: ");
array_push($stack, $element);
echo "Element inserted in stack.\n";
break;
case 2:
if (!empty($stack)) {
$removed = array_pop($stack);
echo "Deleted from stack: $removed\n";
} else {
echo "Stack is empty.\n";
}
break;
case 3:
echo "Stack contents:\n";
print_r(array_reverse($stack)); // Top of stack at the end
break;
case 4:
$element = readline("Enter element to insert in queue: ");
array_push($queue, $element);
echo "Element inserted in queue.\n";
break;
case 5:
if (!empty($queue)) {
$removed = array_shift($queue);
echo "Deleted from queue: $removed\n";
} else {
echo "Queue is empty.\n";
}
break;
case 6:
echo "Queue contents:\n";
print_r($queue);
break;
case 7:
echo "Exiting program.\n";
break;
default:
echo "Invalid choice. Please try again.\n";
}
} while ($choice != 7);
?>
Q.2.
Solution in slip no. 2 Question no 2
*slip no 9
Q.1.
HTML (index.html)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Box Layout</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>This is a header.</header>

<div class="container">
<aside>Look in the box at the right for some information.</aside>
<section>Here is some information.</section>
</div>

<footer>This is a footer.</footer>
</body>
</html>
External CSS (style.css)

body {
margin: 0;
font-family: Arial, sans-serif;
}
header, footer {
border: 2px solid black;
padding: 20px;
font-size: 24px;
font-weight: bold;
background-color: white;
}
.container {
display: flex;
border: 2px groove gray;
}
aside {
width: 25%;
padding: 15px;
border-right: 2px groove gray;
}
section {
width: 75%;
padding: 15px;
}

Q.2.
HTML Form to Accept Title of Event

<!-- event_form.html -->


<form method="POST" action="update_status.php">
<label>Enter Event Title:</label>
<input type="text" name="title" required>
<button type="submit">Update Committee Status</button>
</form>
PHP Script to Update Committee Status

<!-- update_status.php -->


<?php
// DB connection setup (adjust as per your credentials)
$host = "localhost";
$user = "root";
$pass = "";
$dbname = "eventdb"; // Change to your actual DB name
$conn = new mysqli($host, $user, $pass, $dbname);
if ($conn->connect_error) {
die("Connection Failed: " . $conn->connect_error);
}
// Get event title from form
$title = $_POST['title'];
// Get eno of the event
$eventQuery = "SELECT eno FROM Event WHERE title = ?";
$stmt = $conn->prepare($eventQuery);
$stmt->bind_param("s", $title);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$eno = $row['eno'];
// Update committee status to 'working' for all committees in this event
$updateQuery = "
UPDATE Committee
SET status = 'working'
WHERE cno IN (
SELECT cno FROM Event_Committee WHERE eno = ?
)
";
$updateStmt = $conn->prepare($updateQuery);
$updateStmt->bind_param("i", $eno);
if ($updateStmt->execute()) {
echo "Committee status updated to 'working' successfully.";
} else {
echo "Error updating committee status.";
}
} else {
echo "No event found with the title '$title'";
}
$conn->close();
?>
*slip no 9
Q.1.

1. HTML Form (index.html)

<!DOCTYPE html>

<html>

<head>

<title>Math Operations</title>

</head>

<body>

<h2>Enter Two Numbers</h2>

<form method="POST" action="operations.php">

First Number: <input type="number" name="num1" required><br><br>

Second Number: <input type="number" name="num2" required><br><br>

<input type="submit" value="Calculate">

</form>

</body>

</html>

2. PHP Script (operations.php)

<?php

// Get values from form

$num1 = $_POST['num1'];

$num2 = $_POST['num2'];

// Function to find modulus

function findMod($a, $b) {

return $a % $b;

// Function to find power

function findPower($a, $b) {

return pow($a, $b);


}

// Function to find sum of first n numbers

function sumOfN($n) {

return ($n * ($n + 1)) / 2;

// Function to find factorial

function factorial($n) {

$fact = 1;

for ($i = 1; $i <= $n; $i++) {

$fact *= $i;

return $fact;

// Output results

echo "<h2>Results:</h2>";

echo "Modulus of $num1 and $num2: " . findMod($num1, $num2) . "<br>";

echo "Power of $num1 raised to $num2: " . findPower($num1, $num2) . "<br>";

echo "Sum of first $num1 numbers: " . sumOfN($num1) . "<br>";

echo "Factorial of $num2: " . factorial($num2);

?>

Q.2.

<!DOCTYPE html>

<html>

<head>

<title>Spring Temperatures</title>

</head>

<body>

<h2>Spring Month Temperature Stats</h2>


<?php

// Step 1: Array of 30 temperatures (approx. in Celsius)

$temps = [24, 22, 26, 25, 23, 28, 27, 24, 29, 21,

30, 26, 25, 23, 27, 31, 22, 24, 25, 29,

28, 26, 27, 23, 30, 29, 22, 21, 26, 25];

// Step 2: Calculate average

$sum = array_sum($temps);

$average = round($sum / count($temps), 2);

// Step 3: Find 5 warmest (descending sort)

rsort($temps);

$warmest = array_slice($temps, 0, 5);

// Step 4: Find 5 coolest (ascending sort)

sort($temps);

$coolest = array_slice($temps, 0, 5);

// Display results

echo "<strong>Average Temperature:</strong> $average &deg;C <br><br>";

echo "<strong>Five Warmest Days:</strong><br>";

foreach ($warmest as $temp) {

echo "$temp &deg;C<br>";

echo "<br><strong>Five Coolest Days:</strong><br>";

foreach ($coolest as $temp) {

echo "$temp &deg;C<br>";

?>

</body>

</html>
*Slip no 10

Q.1.

<!DOCTYPE html>

<html>

<head>

<title>Integer Operations</title>

</head>

<body>

<h2>Enter Two Numbers</h2>

<form method="post">

First Number: <input type="number" name="num1" required><br><br>

Second Number: <input type="number" name="num2" required><br><br>

<input type="submit" value="Calculate">

</form>

<?php

// Check if form submitted

if ($_SERVER["REQUEST_METHOD"] == "POST") {

// Accept input

$num1 = (int)$_POST["num1"];

$num2 = (int)$_POST["num2"];

// a) Mod function

function findMod($a, $b) {

return $a % $b;

// b) Power function

function findPower($a, $b) {

return pow($a, $b);

}
// c) Sum of first n natural numbers

function sumOfFirstN($n) {

return ($n * ($n + 1)) / 2;

// d) Factorial

function factorial($n) {

$fact = 1;

for ($i = 1; $i <= $n; $i++) {

$fact *= $i;

return $fact;

echo "<h3>Results:</h3>";

echo "Mod ($num1 % $num2): " . findMod($num1, $num2) . "<br>";

echo "Power ($num1 ^ $num2): " . findPower($num1, $num2) . "<br>";

echo "Sum of first $num1 numbers: " . sumOfFirstN($num1) . "<br>";

echo "Factorial of $num2: " . factorial($num2) . "<br>";

?>

</body>

</html>

Q.2.

<!DOCTYPE html>

<html>

<head>

<title>Spring Month Temperature Report</title>

</head>

<body>
<h2>🌸 Spring Month High Temperatures</h2>

<?php

// Step 1: Array of 30 temperatures (Celsius)

$temps = [23, 25, 24, 27, 29, 28, 26, 22, 24, 30,

31, 32, 25, 26, 24, 23, 21, 22, 28, 30,

27, 29, 26, 25, 28, 29, 27, 24, 26, 25];

// Step 2: Calculate average

$total = array_sum($temps);

$average = round($total / count($temps), 2);

// Step 3: Sort descending for 5 warmest

rsort($temps); // Descending

$warmest = array_slice($temps, 0, 5);

// Step 4: Sort ascending for 5 coolest

sort($temps); // Ascending

$coolest = array_slice($temps, 0, 5);

// Display results

echo "<strong>🌡️ Average Temperature:</strong> $average &deg;C<br><br>";

echo "<strong>🔥 Five Warmest Days:</strong><br>";

foreach ($warmest as $temp) {

echo "$temp &deg;C<br>";

echo "<br><strong>❄️ Five Coolest Days:</strong><br>";

foreach ($coolest as $temp) {

echo "$temp &deg;C<br>";

?>

</body>

</html>
*slip no 11

Q.1.

Solution in slip no 5 Question no. 1

Q.2.

<!DOCTYPE html>

<html>

<head>

<title>Associative Array Sorting</title>

</head>

<body>

<h2>Original Associative Array:</h2>

<?php

// Original array

$students = array("Sophia"=>"31", "Jacob"=>"41", "William"=>"39", "Ramesh"=>"40");

echo "<pre>";

print_r($students);

echo "</pre>";

// a) Ascending order sort by VALUE

asort($students);

echo "<h3>a) Ascending Order by Value (asort):</h3>";

echo "<pre>";

print_r($students);

echo "</pre>";

// b) Ascending order sort by KEY

ksort($students);

echo "<h3>b) Ascending Order by Key (ksort):</h3>";

echo "<pre>";

print_r($students);
echo "</pre>";

// c) Descending order sort by VALUE

arsort($students);

echo "<h3>c) Descending Order by Value (arsort):</h3>";

echo "<pre>";

print_r($students);

echo "</pre>";

// d) Descending order sort by KEY

krsort($students);

echo "<h3>d) Descending Order by Key (krsort):</h3>";

echo "<pre>";

print_r($students);

echo "</pre>";

?>

</body>

</html>
*slip no 12

Q.1.

Solution in slip no.4 question no.1


Q.2.
<?php
session_start();
// Initialize stack and queue if not already set
if (!isset($_SESSION['stack'])) {
$_SESSION['stack'] = [];
}
if (!isset($_SESSION['queue'])) {
$_SESSION['queue'] = [];
}
// Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$operation = $_POST['operation'];
$element = $_POST['element'] ?? null;
switch ($operation) {
case "insert_stack":
if ($element !== null && $element !== "")
array_push($_SESSION['stack'], $element);
break;
case "delete_stack":
array_pop($_SESSION['stack']);
break;
case "insert_queue":
if ($element !== null && $element !== "")
array_push($_SESSION['queue'], $element); // FIFO insert at end
break;
case "delete_queue":
array_shift($_SESSION['queue']); // Remove from front
break;
case "reset":
$_SESSION['stack'] = [];
$_SESSION['queue'] = [];
break;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Stack and Queue Operations</title>
</head>
<body>
<h2>Stack and Queue Menu Driven Program</h2>
<form method="post">
<label>Enter Element:</label>
<input type="text" name="element"><br><br>

<label>Select Operation:</label><br>
<input type="radio" name="operation" value="insert_stack" required> a) Insert into
Stack<br>
<input type="radio" name="operation" value="delete_stack"> b) Delete from Stack<br>
<input type="radio" name="operation" value="insert_queue"> d) Insert into Queue<br>
<input type="radio" name="operation" value="delete_queue"> e) Delete from
Queue<br><br>
<input type="submit" value="Perform Operation">
<button type="submit" name="operation" value="reset">Reset All</button>
</form>
<h3>c) Stack Contents (LIFO):</h3>
<pre><?php print_r(array_reverse($_SESSION['stack'])); ?></pre>
<h3>f) Queue Contents (FIFO):</h3>
<pre><?php print_r($_SESSION['queue']); ?></pre>
</body>
</html>
*slip no 13
Q.1.
<!DOCTYPE html>
<html>
<head>
<title>String Operations</title>
</head>
<body>
<h2>Enter Two Strings</h2>
<form method="post">
<label>Large String: </label>
<input type="text" name="large"><br><br>
<label>Small String: </label>
<input type="text" name="small"><br><br>
<label>Enter number of characters (n) for comparison: </label>
<input type="number" name="n"><br><br>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$large = $_POST['large'];
$small = $_POST['small'];
$n = $_POST['n'];
echo "<h3>Results:</h3>";
// a) Check if small string appears at start of large string
if (str_starts_with($large, $small)) {
echo "a) Small string appears at the <strong>start</strong> of the large string.<br>";
} else {
echo "a) Small string does <strong>not</strong> appear at the start.<br>";
}
// b) Find position of small string in large string
$pos = strpos($large, $small);
if ($pos !== false) {
echo "b) Small string found at position: <strong>$pos</strong><br>";
} else {
echo "b) Small string not found in large string.<br>";
}
// c) Compare first n characters (case-insensitive)
$substr_large = strtolower(substr($large, 0, $n));
$substr_small = strtolower(substr($small, 0, $n));
if ($substr_large === $substr_small) {
echo "c) First $n characters are <strong>equal</strong> (case-insensitive
comparison).<br>";
} else {
echo "c) First $n characters are <strong>not equal</strong>.<br>";
}
}
?>
</body>
</html>
Q.2.
<!DOCTYPE html>
<html>
<head>
<title>Search Array</title>
</head>
<body>
<h2>Search for a Value in an Array</h2>
<form method="post">
<label>Enter value to search:</label>
<input type="text" name="searchValue">
<input type="submit" value="Search">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Sample array
$myArray = array("apple", "banana", "orange", "mango", "banana", "grapes", "banana");
$searchValue = $_POST['searchValue'];
// Find all keys where the value matches
$matchingKeys = array_keys($myArray, $searchValue);

if (!empty($matchingKeys)) {
echo "<h3>Value '$searchValue' found at positions: </h3>";
echo implode(", ", $matchingKeys);
} else {
echo "<h3>Value '$searchValue' not found in the array.</h3>";}
}
?>
</body>
</html>
*slip no 14
Q.1.
<!DOCTYPE html>
<html>
<head>
<title>List of Books</title>
</head>
<body>
<h2>List of Books</h2>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th rowspan="2">Item No</th>
<th rowspan="2">Item Name</th>
<th colspan="2">Price</th>
</tr>
<tr>
<th>Rs.</th>
<th>Paise</th>
</tr>
<tr>
<td>1</td>
<td>Programming in Python</td>
<td>500</td>
<td>50</td>
</tr>
<tr>
<td>2</td>
<td>Programming in Java</td>
<td>345</td>
<td>00</td>
</tr>
</table>
</body>
</html>
Q.2.
<!DOCTYPE html>
<html>
<head>
<title>Greeting Generator</title>
</head>
<body>
<h2>Enter Student, College and Greeting</h2>
<form method="post">
<label>Student Name:</label><br>
<input type="text" name="student"><br><br>
<label>College Name:</label><br>
<input type="text" name="college"><br><br>
<label>Greeting Message:</label><br>
<input type="text" name="greeting"><br><br>
<input type="submit" value="Generate Greeting">
</form>
<?php
function greet($student = "Student", $college = "your college", $greeting = "Welcome") {
echo "<h3>$greeting, $student from $college!</h3>";
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$student = !empty($_POST['student']) ? $_POST['student'] : null;
$college = !empty($_POST['college']) ? $_POST['college'] : null;
$greeting = !empty($_POST['greeting']) ? $_POST['greeting'] : null;
greet($student, $college, $greeting);
}
?>
</body>
</html>
*slip no 15
Q.1.
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Pune</title> <!-- v) Title -->
</head>
<body style="background-color: pink;"> <!-- vi) Background color -->
<!-- vii) City name at top -->
<h1 style="color: blue; font-size: 50px; text-align: center;">Pune</h1>
<!-- viii) Scrolling text -->
<marquee style="color: darkred; font-size: 20px;">
Pune is known as the cultural capital of Maharashtra. It is a vibrant city with a rich history.
</marquee>
<!-- iv) Landmark names -->
<h2 style="color: green; font-family: Verdana;">Shaniwar Wada</h2>
<h2 style="color: purple; font-family: Georgia; font-style: italic;">Aga Khan Palace</h2>
<h2 style="color: brown; font-family: Arial; font-weight: bold;">Sinhagad Fort</h2>
<h2 style="color: teal; font-family: Courier New; text-decoration: underline;">Dagadusheth
Halwai Ganapati Temple</h2>
<!-- viii) Image at the bottom -->
<div style="text-align: center; margin-top: 50px;">
<img
src="https://upload.wikimedia.org/wikipedia/commons/e/e5/Shaniwarwada_Pune.jpg"
alt="Pune Landmark"
style="width: 400px; height: auto; border: 3px solid black;" > </div>
</body>
</html>
Q.2.
<!DOCTYPE html>
<html>
<head>
<title>Stack and Queue Operations</title>
</head>
<body>
<h2>Stack and Queue Operations</h2>
<form method="post">
<label>Select Operation:</label><br><br>
<!-- Stack operations -->
<input type="radio" name="choice" value="1"> Insert into Stack<br>
<input type="radio" name="choice" value="2"> Delete from Stack<br>
<input type="radio" name="choice" value="3"> Display Stack<br><br>
<!-- Queue operations -->
<input type="radio" name="choice" value="4"> Insert into Queue<br>
<input type="radio" name="choice" value="5"> Delete from Queue<br>
<input type="radio" name="choice" value="6"> Display Queue<br><br>
<label>Enter Element (for insert):</label><br>
<input type="text" name="element"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
session_start();
// Initialize stack and queue in session if not already
if (!isset($_SESSION['stack'])) {
$_SESSION['stack'] = [];
}
if (!isset($_SESSION['queue'])) {
$_SESSION['queue'] = [];
}
// Handle form submission
if (isset($_POST['submit'])) {
$choice = $_POST['choice'];
$element = $_POST['element'];
switch ($choice) {
case "1": // Insert into stack
if (!empty($element)) {
array_push($_SESSION['stack'], $element);
echo "<p>Inserted '$element' into stack.</p>";
}
break;
case "2": // Delete from stack
if (!empty($_SESSION['stack'])) {
$deleted = array_pop($_SESSION['stack']);
echo "<p>Deleted '$deleted' from stack.</p>";
} else {
echo "<p>Stack is empty.</p>";
}
break;
case "3": // Display stack
echo "<p><strong>Stack contents:</strong><br>";
$stack = array_reverse($_SESSION['stack']); // Display in LIFO
foreach ($stack as $item) {
echo "$item<br>";
}
echo "</p>";
break;
case "4": // Insert into queue
if (!empty($element)) {
array_push($_SESSION['queue'], $element);
echo "<p>Inserted '$element' into queue.</p>";
}
break;
case "5": // Delete from queue
if (!empty($_SESSION['queue'])) {
$deleted = array_shift($_SESSION['queue']);
echo "<p>Deleted '$deleted' from queue.</p>";
} else {
echo "<p>Queue is empty.</p>";
}
break;
case "6": // Display queue
echo "<p><strong>Queue contents:</strong><br>";
foreach ($_SESSION['queue'] as $item) {
echo "$item<br>";
}
echo "</p>";
break;
default:
echo "<p>Please select an option.</p>";
}
}
?>

</body>
</html>

You might also like