IWD4340704
IWD4340704
Practical :- 2(A)
CODE:-
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$operation = $_POST['operation'];
switch ($operation) {
case 'add':
$result = $num1 + $num2;
break;
case 'subtract':
$result = $num1 - $num2;
break;
case 'multiply':
$result = $num1 * $num2;
break;
case 'divide':
if ($num2 != 0) {
$result = $num1 / $num2;
} else {
$result = "Cannot divide by zero";
}
break;
default:
$result = "Invalid operation";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Simple Calculator</title>
</head>
<body>
<h1>Simple Calculator</h1>
<form method="post" action="">
<input type="number" name="num1" required placeholder="Enter first
number">
<input type="number" name="num2" required placeholder="Enter second
number">
<select name="operation" required>
<option value="add">Add</option>
<option value="subtract">Subtract</option>
<option value="multiply">Multiply</option>
<option value="divide">Divide</option>
</select>
<input type="submit" value="Calculate">
</form>
<?php
if (isset($result)) {
echo "<h2>Result: $result</h2>";
}
?>
OUTPUT-:
IWD4340704
Practical :- 3(A)
Aim: - Write a script that reads the name of the car and
displays the name of the
company the car belongs to
as per the below table:
car company
Safari,nexon,tiago,tigor Tata
XUV700,XUV300,Bolero Mahindra
I20,verna,venue,creta Hyundai
Swift,alto,baleno,brezza Suzuki
CODE: -
<?php
// Define an associative array where car names are keys and companies are values
$carCompanies = [
'Safari' => 'Tata', 'nexon' => 'Tata', 'tiago' => 'Tata', 'tigor' => 'Tata',
'XUV700' => 'Mahindra', 'XUV300' => 'Mahindra', 'Bolero' => 'Mahindra',
'I20' => 'Hyundai', 'verna' => 'Hyundai', 'venue' => 'Hyundai', 'creta' =>
'Hyundai',
'Swift' => 'Suzuki', 'alto' => 'Suzuki', 'baleno' => 'Suzuki', 'brezza' => 'Suzuki'
];
// Check if the car name exists in the array and display the company name
if (array_key_exists($carName, $carCompanies)) {
$response = "The company of the car '{$carName}' is: " .
$carCompanies[$carName];
} else {
$response = "Car not found in the database.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Car Company Finder</title>
</head>
<body>
<h1>Find the Company of a Car</h1>
<h2>Result:</h2>
<p><?php echo $response; ?></p>
</body>
</html>
OUTPUT-:
IWD4340704
Practical :- 3(B)
<!DOCTYPE html>
<html>
<head>
<title>Fibonacci Sequence</title>
</head>
<body>
<h1>Fibonacci Sequence Generator</h1>
<form method="post" action="">
<input type="number" name="terms" required placeholder="Enter number
of terms">
<input type="submit" value="Generate">
</form>
<?php
if (isset($fibonacci)) {
echo "<h2>Fibonacci Sequence:</h2>";
echo implode(", ", array_slice($fibonacci, 0, $terms));
}
?>
</body>
</html>
OUTPUT- :
IWD4340704
Practical :- 3(C)
CODE-:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST['number'];
$table = [];
<!DOCTYPE html>
<html>
<head>
<title>Multiplication Table</title>
</head>
<body>
<h1>Multiplication Table Generator</h1>
<form method="post" action="">
<input type="number" name="number" required
placeholder="Enter a number">
<input type="submit" value="Generate Table">
</form>
<?php
if (isset($table)) {
echo "<h2>Multiplication Table for $number:</h2>";
echo "<ul>";
foreach ($table as $line) {
echo "<li>$line</li>";
}
echo "</ul>";
}
?>
</body>
</html>
OUTPUT-:
IWD4340704
Practical :- 4(A)
CODE-:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$inputString = $_POST['inputString'];
$length = 0;
$wordCount = 0;
$inWord = false;
<!DOCTYPE html>
<html>
<head>
<title>String Analysis</title>
</head>
<body>
<h1>String Length and Word Count</h1>
<form method="post" action="">
<textarea name="inputString" required placeholder="Enter
a string"></textarea>
<input type="submit" value="Analyze">
</form>
<?php
if (isset($length) && isset($wordCount)) {
echo "<h2>Results:</h2>";
echo "Length of the string: $length<br>";
echo "Number of words: $wordCount";
}
?>
</body>
</html>
OUTPUT-:
CODE-:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$inputArray = explode(',', $_POST['inputArray']);
$length = count($inputArray);
<!DOCTYPE html>
<html>
<head>
<title>Array Sorter</title>
</head>
<body>
<h1>Sort an Indexed Array</h1>
<form method="post" action="">
<input type="text" name="inputArray" required
placeholder="Enter numbers separated by commas">
<input type="submit" value="Sort">
</form>
<?php
if (isset($inputArray)) {
echo "<h2>Sorted Array:</h2>";
echo implode(", ", $inputArray);
}
?>
</body>
</html>
OUTPUT-:
IWD4340704
Practical :- 4(C)
<!DOCTYPE html>
<html>
<head>
<title>3x3 Matrix Addition and Multiplication</title>
</head>
<body>
<h2>3x3 Matrix Operations</h2>
<form method="post">
<h3>Matrix A</h3>
<?php
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
$name = "a{$i}{$j}";
$value = $_POST[$name] ?? '';
echo "<input type='number' name='$name'
value='$value' required>";
}
echo "<br>";
}
?>
<h3>Matrix B</h3>
<?php
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
$name = "b{$i}{$j}";
$value = $_POST[$name] ?? '';
echo "<input type='number' name='$name'
value='$value' required>";
}
echo "<br>";
}
?>
<br>
<input type="submit" name="submit"
value="Calculate">
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Create matrices A and B
$A = $B = [];
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
$A[$i][$j] = (int)$_POST["a{$i}{$j}"];
$B[$i][$j] = (int)$_POST["b{$i}{$j}"];
}
}
// Addition
$sum = [];
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
$sum[$i][$j] = $A[$i][$j] + $B[$i][$j];
}
}
// Multiplication
$product = [];
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
$product[$i][$j] = 0;
for ($k = 0; $k < 3; $k++) {
$product[$i][$j] += $A[$i][$k] * $B[$k][$j];
}
}
}
// Print results
printMatrix($sum, "Matrix A + B (Addition)");
printMatrix($product, "Matrix A x B (Multiplication)");
}
?>
</body>
</html>
OUTPUT-:
IWD4340704
Practical :- 4(D)
$message = strtoupper($message);
$encoded = "";
return trim($encoded);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Morse Code Encoder</title>
</head>
<body>
<h2>Morse Code Encoder</h2>
<form method="post">
<label>Enter your message:</label><br>
<input type="text" name="message" required
style="width: 300px;"><br><br>
<input type="submit" value="Encode">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$input = $_POST["message"];
$output = encodeToMorse($input);
echo "<h3>Encoded Morse Code:</h3>";
echo "<p><strong>" . htmlspecialchars($output) .
"</strong></p>";
}
?>
</body>
</html>
OUTPUT-:
IWD4340704
Practical :- 5(A)
function reverseString($str) {
return strrev($str);
}
function removeWhitespaces($str) {
return str_replace(' ', '', $str);
}
<!DOCTYPE html>
<html>
<head>
<title>String Function Utilities</title>
</head>
<body>
<h2>PHP String Operations</h2>
<form method="post">
<label>Enter a string:</label><br>
<input type="text" name="inputString" required
style="width: 300px;"><br><br>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$input = $_POST["inputString"];
$search = $_POST["search"] ?? '';
$replace = $_POST["replace"] ?? '';
echo "<h3>Results:</h3>";
// a) Check if lowercase
echo "<p><strong>a) Is lowercase?</strong> " .
(isLowercase($input) ? "Yes" : "No") . "</p>";
// b) Reverse string
echo "<p><strong>b) Reversed string:</strong> " .
reverseString($input) . "</p>";
// d) Replace word
if (!empty($search) && isset($replace)) {
echo "<p><strong>d) After replacing '{$search}'
with '{$replace}':</strong> " . replaceWord($input,
$search, $replace) . "</p>";
} else {
echo "<p><strong>d)</strong> No replacement
done (empty input).</p>";
}
}
?>
</body>
</html>
OUTPUT-:
IWD4340704
Practical :- 5(B)
<!DOCTYPE html>
<html>
<head>
<title>PHP Math Functions</title>
</head>
<body>
<h2>PHP Math Functions</h2>
<form method="post">
<!-- Random number range -->
<label>Enter the range for random number (min and
max):</label><br>
<input type="number" name="min" required
placeholder="Min Value">
<input type="number" name="max" required
placeholder="Max Value"><br><br>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// a) Random number between given range
$min = $_POST["min"];
$max = $_POST["max"];
$randomNumber = generateRandomNumber($min, $max);
echo "<p><strong>a) Random number between $min and
$max: </strong>$randomNumber</p>";
// c) Trigonometric functions
$angle = $_POST["angle"];
list($sinValue, $cosValue, $tanValue) =
trigFunctions($angle);
echo "<p><strong>c) Trigonometric values for angle
$angle°:</strong></p>";
echo "<ul>";
echo "<li>Sin: $sinValue</li>";
echo "<li>Cos: $cosValue</li>";
echo "<li>Tan: $tanValue</li>";
echo "</ul>";
}
?>
</body>
</html>
OUTPUT-:
IWD4340704
Practical :- 6(A)
// Public method
public function greet() {
echo "Hello, my name is {$this->name} and I am {$this-
>age} years old.<br>";
}
// ii) Create an object of the class and access its public properties
and methods
// Creating an object of the 'Person' class
$person1 = new Person("John", 25);
Practical :- 6(B)
Aim: - Write a script that uses the set attribute and get
attribute methods to access a class’s private attributes
of a class.
CODE-:
<?php
// Class definition with private attributes
class Person {
// Private attributes
private $name;
private $age;
interface AnimalInterface {
public function speak();
}
interface MammalInterface {
public function walk();
}
class Vehicle {
public function start() {
return "Vehicle is starting.";
}
}
CODE-:
<?php
// Parent Class (Base Class)
class Animal {
public function speak($sound = null) {
if ($sound === null) {
return "The animal makes a sound.";
} else {
return "The animal says: " . $sound;
}
}
}
// Overriding Example
$dog = new Dog();
echo "<h3>Method Overriding:</h3>";
echo $dog->speak() . "<br>"; // Overridden method
echo $dog->speak("Woof!") . "<br>"; // Overridden method with
argument
// Overloading Example
$calculator = new Calculator();
echo "<h3>Method Overloading:</h3>";
echo "Square of 5: " . $calculator->add(5) . "<br>"; // Overload
with 1 argument
echo "Sum of 5 and 10: " . $calculator->add(5, 10) . "<br>"; //
Overload with 2 arguments
echo "Sum of 1, 2, 3, 4: " . $calculator->add(1, 2, 3, 4) . "<br>"; //
Overload with more than 2 arguments
?>
OUTPUT-:
IWD4340704
Practical :- 6(E)