OST Lab Record
OST Lab Record
STUDENT NAME :
REGISTER NUMBER :
APIRL 2025
SRM INSTITUTE OF SCIENCE AND TECHNOLOGY
FACULTY OF SCIENCE AND HUMANITIES
DEPARTMENT OF COMPUTER APPLICATIONS
SRM Nagar, Kattankulathur – 603 203
CERTIFICATE
S. PAGE STAFF
DATE TITLE
NO NO SIGNATURE
2
(B) CHECK IF A NUMBER IS POSITIVE,
09/12/2024
NEGATIVE, OR ZERO
PAGE STAFF
S. NO DATE TITLE
NO SIGNATURE
3. To create a directory
Command: mkdir <directory_name>
Command: ls -lr
Command: cd <directory_name>
Command: ls -lh
Aim :
Procedure :
Program :
<?php
$num = 7;
if ($num % 2 == 0) {
echo "$num is Even";
} else {
echo "$num is Odd";
}
?>
Output :
7 is Odd
Result :
Aim :
Procedure :
Program :
<?php
$num = -5;
if ($num > 0) {
echo "$num is Positive";
} elseif ($num < 0) {
echo "$num is Negative";
} else {
echo "$num is Zero";
}
?>
Output :
-5 is Negative
Result :
Aim :
To write a PHP program that prints even numbers from 1 to 20 using a for loop.
Procedure :
Program :
<?php
for ($i = 2; $i <= 20; $i += 2) {
echo "$i ";
}
?>
Output :
2 4 6 8 10 12 14 16 18 20
Result :
Aim :
To write a PHP program that calculates the sum of the first 10 natural numbers
using a while loop.
Procedure :
Program :
<?php
$sum = 0;
$i = 1;
while ($i <= 10) {
$sum += $i;
$i++;
}
echo "Sum of first 10 natural numbers is: $sum";
?>
Output :
Result :
Aim :
To write a PHP program that prints the multiplication table of 5 using a do-
while loop.
Procedure :
Program :
<?php
$num = 5;
$i = 1;
do {
echo "$num x $i = " . ($num * $i) . "<br>";
$i++;
} while ($i <= 10);
?>
Output :
5 × 1 = 5
5 × 2 = 10
5 × 3 = 15
5 × 4 = 20
5 × 5 = 25
5 × 6 = 30
5 × 7 = 35
5 × 8 = 40
5 × 9 = 45
5 × 10 = 50
Result :
Aim :
To write a PHP program that stores and displays five student names using a
one-dimensional array.
Procedure :
Program :
<?php
$students = array("John", "Alice", "Mike", "Sara", "Tom");
foreach ($students as $student) {
echo "$student <br>";
}
?>
Output :
John
Alice
Mike
Sara
Tom
Result :
Aim :
To write a PHP program that stores and displays student marks using an
associative array.
Procedure :
Program :
<?php
$marks = array("John" => 85, "Alice" => 90, "Mike" => 78);
foreach ($marks as $name => $score) {
echo "$name scored $score marks.<br>";
}
?>
Output :
Result :
Aim :
To write a PHP program that stores and displays student details using a
multidimensional array.
Procedure :
Program :
<?php
$students = array(
array("John", 85, "A"),
array("Alice", 90, "A+"),
array("Mike", 78, "B")
);
foreach ($students as $student) {
echo "Name: " . $student[0] . ", Marks: " .
$student[1] . ", Grade: " . $student[2] . "<br>";
}
?>
Output :
Result :
Aim :
Procedure :
Program :
<?php
$numbers = array(5, 2, 8, 1, 3);
sort($numbers);
echo "Sorted numbers: ";
foreach ($numbers as $num) {
echo "$num ";
}
?>
Output :
Sorted numbers: 1 2 3 5 8
Result :
Aim :
To write a PHP program to find the maximum and minimum values in an array.
Procedure :
Program :
<?php
$numbers = array(10, 20, 5, 40, 30);
$max = max($numbers);
$min = min($numbers);
echo "Maximum value: $max <br>";
echo "Minimum value: $min";
?>
Output :
Maximum value: 40
Minimum value: 5
Result :
Aim :
To create a class in PHP that calculates and prints the factorial of a given
number.
Procedure :
Program :
<?php
class factorial_of_a_number {
protected $n;
Output :
120
Result :
Aim :
To calculate the difference between two dates using PHP's DateTime class
Procedure :
Program :
<?php
$sdate = new DateTime("1981-11-03");
$edate = new DateTime("2013-09-04");
$interval = $sdate->diff($edate);
echo "Difference : " . $interval->y . " years, " .
$interval->m . " months, " . $interval->d . " days ";
?>
Output :
120
Result :
Aim :
Procedure :
Program :
<?php
class BankAccount {
private $balance = 0;
Output :
105
Result :
Aim :
Procedure :
Program :
<?php
$r= "I am";
echo "$r\n";
echo strlen("Hi Everyone")."\n";
echo str_word_count("Hi Everyone")."\n";
echo strrev("Hello World")."\n";
echo strpos("Hi Buddy", "Buddy")."\n";
echo str_replace("world", "Jack", "Hello world")."\n";
echo strtoupper("world")."\n";
echo strtolower("WORLD")."\n";
echo ucfirst("world")."\n";
echo lcfirst("WORLD")."\n";
echo str_word_count("Welcome to PHP programming")."\n";
echo substr_replace("Hello World", "Everyone", 6)."\n";
echo substr_replace("alone", "ph", 0, 2)."\n";
?>
Output :
I am
12
2
dlroW olleH
3
Hello Jack
WORLD
world
World
wORLD
4
Hello Everyone
phone
Result :
Aim :
Procedure :
Program :
Call by value :
<?php
function incrementValue($num) {
$num += 10;
echo "Inside function: $num\n";
}
$number = 10;
incrementValue($number);
echo "Outside function: $number\n";
?>
Output :
Inside function: 20
Outside function: 10
Call by reference :
<?php
function incrementValueByReference(&$num) {
$num += 10;
echo "Inside function: $num\n";
}
$number = 10;
incrementValueByReference($number);
echo "Outside function: $number\n";
?>
Output :
Inside function: 20
Outside function: 20
Result :
Aim :
Procedure :
Program :
<?php
function checkemail($str) {
return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-
9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE :
TRUE;
}
if (!checkemail("[email protected]")) {
echo "Invalid email address.";
} else {
echo "Valid email address.";
}
?>
Output :
Valid email address.
Result :
Aim :
Procedure :
Program :
<?php
$phone = "9898203462";
if (preg_match('/^[0-9]{10}$/', $phone)) {
echo "Valid Phone Number";
} else {
echo "Invalid Phone Number";
}
?>
Output :
Valid Phone Number
Result :
Aim :
Procedure :
Program :
<?php
// Database Connection
$conn = new mysqli("localhost", "root", "", "newDB");
if ($conn->connect_error) {
die("Connection failure: " . $conn->connect_error);
}
// Create Table
$sql = "CREATE TABLE IF NOT EXISTS employees (
id INT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL
)";
if ($conn->query($sql) === TRUE) {
echo "Table created successfully.<br>";
} else {
echo "Error creating table: " . $conn->error . "<br>";
}
// Insert Data
$sql = "INSERT INTO employees (id, firstname, lastname, email)
VALUES (1, 'Ram', 'Singh', '[email protected]')
ON DUPLICATE KEY UPDATE firstname='Ram', lastname='Singh', email='[email protected]'";
if ($conn->query($sql) === TRUE) {
echo "Record inserted successfully.<br>";
} else {
echo "Error inserting record: " . $conn->error . "<br>";
}
// Update Data
$sql = "UPDATE employees SET email='[email protected]' WHERE id=1";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully.<br>";
} else {
echo "Error updating record: " . $conn->error . "<br>";
}
// Delete Data
$sql = "DELETE FROM employees WHERE id=1";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully.<br>";
} else {
echo "Error deleting record: " . $conn->error . "<br>";
}
// Close Connection
$conn->close();
?>
Output :
Result :
Aim :
To take user input in CLI mode using readline() and display the entered string.
Procedure :
Program :
<?php
?>
Output :
Enter a string: Hello World
You entered: Hello World
Result :
Aim :
Procedure :
Program :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Factorial Calculator</title>
<style>
body { font-family: Arial, sans-serif; text-align:
center; background: lightgray; }
.container { background: white; padding: 20px;
border-radius: 10px; width: 300px; margin: auto; margin-top:
50px; box-shadow: 2px 2px 10px gray; }
input, button { padding: 8px; margin-top: 10px;
border: none; border-radius: 5px; }
button { background: green; color: white; cursor:
pointer; }
</style>
</head>
<body>
<div class="container">
<h2>Factorial Calculator</h2>
<form method="post">
<input type="number" name="num"
placeholder="Enter a number" required>
<button type="submit"
name="submit">Calculate</button>
</form>
<?php
if (!empty($_POST["num"])) {
function fact($n) { return ($n <= 1) ? 1 :
$n * fact($n - 1); }
echo "<h3>Factorial: " . fact($_POST["num"])
. "</h3>";
}
?>
</div>
</body>
</html>
Output :
Result :