Ass 2
Ass 2
Code 2:
<?php
if (isset($_GET['number'])) {
$number = $_GET['number'];
if (is_numeric($number)) {
if ($number > 0) {
$message = "The number $number is positive.";
} elseif ($number < 0) {
$message = "The number $number is negative.";
} else {
$message = "The number is zero.";
}
} else {
$message = "Please enter a valid numeric value.";
}
} else {
$message = "No number provided.";
}
?>
<html>
<head>
<title>Result</title>
</head>
<body>
<h2>Result</h2>
<p><?php echo $message; ?></p>
<a href="1.php"></a>
</body>
</html>
Output:
2. Create a form to accept Username and electricity units consumed. You need to write a PHP
script to calculate the electricity bill using if-else conditions.
Conditions:
For first 50 units – Rs. 3.50/unit
For next 100 units – Rs. 4.00/unit
For next 100 units – Rs. 5.20/unit
For units above 250 – Rs. 6.50/unit
Display the bill amount along with the username. Use HTML formatting tags.
Code:
<html>
<head>
<title>Electricity Bill Calculator</title>
</head>
<body>
<form action="electricity.php" method="get">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
Code 2:
<?php
if (isset($_GET['username']) && isset($_GET['units'])) {
$username = $_GET['username'];
$units = $_GET['units'];
$bill = 0;
if (is_numeric($units) && $units >= 0) {
if ($units <= 50) {
$bill = $units * 3.50;
} elseif ($units <= 150) {
$bill = (50 * 3.50) + (($units - 50) * 4.00);
} elseif ($units <= 250) {
$bill = (50 * 3.50) + (100 * 4.00) + (($units - 150) * 5.20);
} else {
$bill = (50 * 3.50) + (100 * 4.00) + (100 * 5.20) + (($units - 250) * 6.50);
}
echo "<h3>Bill Details</h3>";
echo "<p>Username: <strong>$username</strong></p>";
echo "<p>Electricity Units Consumed: <strong>$units</strong></p>";
echo "<p>Total Bill Amount: <strong>Rs. " . number_format($bill, 2) . "</strong></p>";
} else {
echo "<p>Please enter a valid number of units.</p>";
}
}
?>
<html>
<head>
<title>Result</title>
</head>
<body>
<a href="2.php"></a>
</body>
</html>
Output:
3. Create a form to accept the day number of the week (1-7) and display the day of the week.
Eg if user inputs: 1 output: “Sunday”
Code 1:
<html>
<head>
<title>Day of the Week</title>
</head>
<body>
<form action="days.php" method="get">
<label for="day">Day Number:</label>
<input type="number" id="day" name="day" min="1" max="7" required><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Code 2:
<?php
if (isset($_GET['day'])) {
$day = $_GET['day'];
if (is_numeric($day) && $day >= 1 && $day <= 7) {
$daysOfWeek = [
1 => "Sunday",
2 => "Monday",
3 => "Tuesday",
4 => "Wednesday",
5 => "Thursday",
6 => "Friday",
7 => "Saturday"
];
echo "<h3>Day of the Week</h3>";
echo "<p>The day corresponding to number <strong>$day</strong> is
<strong>{$daysOfWeek[$day]}</strong>.</p>";
} else {
echo "<p>Please enter a valid day number between 1 and 7.</p>";
}
}?>
<html>
<head>
<title>Result</title>
</head>
<body>
<a href="3.php"></a>
</body>
</html>
Output:
4. Create a form to accept a numeric value from the user. Write a PHP program to calculate
the factorial of a number.
Code 1:
<html>
<head>
<title>Factorial Calculator</title>
</head>
<body>
<form action="calculator.php" method="get">
<label for="number">Enter a Number:</label>
<input type="number" id="number" name="number" required><br><br>
<input type="submit" value="Calculate Factorial">
</form>
</body>
</html>
Code 2:
<?php
if (isset($_GET['number'])) {
$number = $_GET['number'];
if (is_numeric($number) && $number >= 0) {
function factorial($n) {
if ($n == 0) {
return 1;
} else {
return $n * factorial($n - 1);
}
}
$result = factorial($number);
echo "<h3>Factorial Result</h3>";
echo "<p>The factorial of <strong>$number</strong> is
<strong>$result</strong>.</p>";
} else {
echo "<p>Please enter a valid non-negative number.</p>";
}
}?>
<html>
<head>
<title>Result</title>
</head>
<body>
<a href="4.php"></a>
</body>
</html>
Output:
Code 2:
<?php
if (isset($_GET['number1']) && isset($_GET['number2']) && isset($_GET['operation'])) {
$number1 = $_GET['number1'];
$number2 = $_GET['number2'];
$operation = $_GET['operation'];
$result = '';
switch ($operation) {
case 'add':
$result = $number1 + $number2;
break;
case 'subtract':
$result = $number1 - $number2;
break;
case 'multiply':
$result = $number1 * $number2;
break;
case 'divide':
if ($number2 != 0) {
$result = $number1 / $number2;
} else {
$result = 'Division by zero is not allowed.';
}
break;
default:
$result = 'Invalid operation selected.';
break;
}
echo "<h3>Calculation Result</h3>";
echo "<p>The result of the operation is: <strong>$result</strong></p>";
}
?>
<html>
<head>
<title>Result</title>
</head>
<body>
<a href="5.php"></a>
</body>
</html>
Output:
Code 2:
<?php
if (isset($_GET['fullname']) && isset($_GET['address']) && isset($_GET['city']) &&
isset($_GET['gender']) && isset($_GET['email']) && isset($_GET['hobbies'])) {
$fullname = $_GET['fullname'];
$address = $_GET['address'];
$city = $_GET['city'];
$gender = $_GET['gender'];
$email = $_GET['email'];
$hobbies = $_GET['hobbies'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<h3>User Details</h3>";
echo "<p><strong>Full Name:</strong> $fullname</p>";
echo "<p><strong>Address:</strong> $address</p>";
echo "<p><strong>City:</strong> $city</p>";
echo "<p><strong>Gender:</strong> $gender</p>";
echo "<p><strong>Email ID:</strong> $email</p>";
echo "<p><strong>Hobbies:</strong> " . implode(', ', $hobbies) . "</p>";
} else {
echo "<p>Invalid email address.</p>";
}
}
}
?>
<html>
<head>
<title>Result</title>
</head>
<body>
<a href="6.php"></a>
</body>
</html>
Output:
7. Create a HTML webpage to accept username, password and contact number from the user.
Write a php script to validate the following using regular expressions as follows:
a. Username: min 5 characters(alphanumeric), max 20 characters(alphanumeric),
only _ and @ allowed.
b. Password: min 5 characters(alphanumeric), max 20 characters(alphanumeric),
must have min one capital letter and one special character.
c. Contact Number: 10 digit contact number.
Code 1:
<html>
<head>
<title>User Validation Form</title>
</head>
<body>
<form action="valid.php" method="get">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<label for="contact">Contact Number:</label>
<input type="text" id="contact" name="contact" required><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Code 2:
<?php
if (isset($_GET['username']) && isset($_GET['password']) && isset($_GET['contact'])) {
$username = $_GET['username'];
$password = $_GET['password'];
$contact = $_GET['contact'];
$usernamePattern = "/^[a-zA-Z0-9_@]{5,20}$/";
$passwordPattern = "/^(?=.*[A-Z])(?=.*[\W_])[a-zA-Z0-9\W_]{5,20}$/";
$contactPattern = "/^\d{10}$/";
$isUsernameValid = preg_match($usernamePattern, $username);
$isPasswordValid = preg_match($passwordPattern, $password);
$isContactValid = preg_match($contactPattern, $contact);
if ($isUsernameValid && $isPasswordValid && $isContactValid) {
echo "<h3>Validation Successful</h3>";
echo "<p><strong>Username:</strong> $username</p>";
echo "<p><strong>Password:</strong> $password</p>";
echo "<p><strong>Contact Number:</strong> $contact</p>";
} else {
echo "<h3>Validation Errors</h3>";
if (!$isUsernameValid) {
echo "<p>Username must be 5-20 characters long, alphanumeric, and can include _
and @.</p>";
}
if (!$isPasswordValid) {
echo "<p>Password must be 5-20 characters long, contain at least one capital letter
and one special character.</p>";
}
if (!$isContactValid) {
echo "<p>Contact number must be exactly 10 digits.</p>";
}
}
}
?>
<html>
<head>
<title>Result</title>
</head>
<body>
<a href="7.php"></a>
</body>
</html>
Output:
8. Write a PHP script to create and display any associative array having Country names and
their capitals as key-value pairs. Add a minimum 7 key-value pairs in the array.
Code:
<html>
<head>
<title>Country and Capital</title>
</head>
<body>
<h2>Country and Capital Information</h2>
<?php
$countriesAndCapitals = array(
"USA" => "Washington, D.C.",
"France" => "Paris",
"Germany" => "Berlin",
"Japan" => "Tokyo",
"Australia" => "Canberra",
"Brazil" => "Brasília",
"Canada" => "Ottawa"
);
echo "<table border='1' cellpadding='10'>";
echo "<tr><th>Country</th><th>Capital</th></tr>";
foreach ($countriesAndCapitals as $country => $capital) {
echo "<tr><td>$country</td><td>$capital</td></tr>";
}
echo "</table>";
?>
</body>
</html>
Output:
9. Create a HTML page which accepts an array item. Write a PHP script that inserts this new
item into the array at any position.
Code 1:
<html>
<head>
<title>Insert Item into Array</title>
</head>
<body>
<form action="array.php" method="get">
<label for="item">New Item:</label>
<input type="text" id="item" name="item" required><br><br>
<label for="position">Position (0-based index):</label>
<input type="number" id="position" name="position" min="0" required><br><br>
<input type="submit" value="Insert Item">
</form>
</body>
</html>
Code 2:
<?php
$array = array("Apple", "Banana", "Cherry", "Date", "Elderberry");
echo "<h3>Original Array</h3>";
echo "<p>" . implode(", ", $array) . "</p>";
if (isset($_GET['item']) && isset($_GET['position'])) {
$newItem = $_GET['item'];
$position = intval($_GET['position']);
if ($position < 0 || $position > count($array)) {
echo "<p>Invalid position. Please enter a position between 0 and " . count($array) .
".</p>";
} else {
array_splice($array, $position, 0, $newItem);
echo "<h3>Updated Array</h3>";
echo "<p>" . implode(", ", $array) . "</p>";
}}?>
<html>
<head>
<title>Result</title>
</head>
<body>
<a href="9.php"></a>
</body>
</html>
Output:
10. Write a PHP script to create an array to store the employee name (key) and their salary
(value), of 5 employees, in an associative array. Create a HTML page to provide the
following options to the user:
a. Display an array
b. Sort an array based on Key
c. Sort an array based on Value
d. Sort an array based on Key in reverse
e. Sort an array based on Value in reverse
Perform appropriate operations depending upon the option selected by the user.
Code 1:
<html>
<head>
<title>Employee Salary Management</title>
</head>
<body>
<form action="emp.php" method="get">
<label for="action">Choose an action:</label>
<select id="action" name="action" required>
<option value="display">Display Array</option>
<option value="sortKey">Sort by Key</option>
<option value="sortValue">Sort by Value</option>
<option value="sortKeyReverse">Sort by Key (Reverse)</option>
<option value="sortValueReverse">Sort by Value (Reverse)</option>
</select><br><br>
<input type="submit" value="Perform Action">
</form>
</body>
</html>
Code 2:
<?php
$employees = array(
"John" => 50000,
"Alice" => 55000,
"Bob" => 45000,
"Charlie" => 60000,
"Diana" => 52000
);
function displayArray($array) {
echo "<h3>Employee Salary Array</h3>";
echo "<table border='1' cellpadding='10'>";
echo "<tr><th>Employee</th><th>Salary</th></tr>";
foreach ($array as $name => $salary) {
echo "<tr><td>$name</td><td>$salary</td></tr>";
}
echo "</table>";
}
if (isset($_GET['action'])) {
$action = $_GET['action'];
switch ($action) {
case 'display':
displayArray($employees);
break;
case 'sortKey':
ksort($employees);
displayArray($employees);
break;
case 'sortValue':
asort($employees);
displayArray($employees);
break;
case 'sortKeyReverse':
krsort($employees);
displayArray($employees);
break;
case 'sortValueReverse':
arsort($employees);
displayArray($employees);
break;
default:
echo "<p>Invalid action selected.</p>";
break;
}
}
?>
<html>
<head>
<title>Result</title>
</head>
<body>
<a href="10.php"></a>
</body>
</html>
Output:
1. 2. 3.
4. 5.
11. Write a PHP script which creates an indexed array having values from 155 to 200 Display
all the array values that are divisible by 4.
Code:
<html lang="en">
<head>
<title>Array Divisibility Check</title>
</head>
<body>
<h2>Values Divisible by 4</h2>
<?php
$values = range(155, 200);
$divisibleByFour = array_filter($values, function($value) {
return $value % 4 == 0;
});
if (!empty($divisibleByFour)) {
echo "<p>Values divisible by 4:</p>";
echo "<ul>";
foreach ($divisibleByFour as $value) {
echo "<li>$value</li>";
}
echo "</ul>";
} else {
echo "<p>No values are divisible by 4.</p>";
}
?>
</body>
</html>
Output:
12. Create a multidimensional array to store details of 5 students in the following format:
allStudents -> Student1 -> stud_detail(name, email)-> subjects (sub_name -> marks, key value
pair for 3 subjects).
Code:
<html>
<head>
<title>Student Details</title>
</head>
<body>
<h2>Student Details</h2>
<?php
$allStudents = array(
"Student1" => array(
"stud_detail" => array(
"name" => "John Doe",
"email" => "[email protected]"),
"subjects" => array(
"Math" => 85,
"Science" => 90,
"English" => 78)),
"Student2" => array(
"stud_detail" => array(
"name" => "Alice Smith",
"email" => "[email protected]"),
"subjects" => array(
"Math" => 88,
"Science" => 92,
"English" => 81)),
"Student3" => array(
"stud_detail" => array(
"name" => "Bob Brown",
"email" => "[email protected]"),
"subjects" => array(
"Math" => 79,
"Science" => 85,
"English" => 87)),
"Student4" => array(
"stud_detail" => array(
"name" => "Charlie Johnson",
"email" => "[email protected]"),
"subjects" => array(
"Math" => 92,
"Science" => 88,
"English" => 84)),
"Student5" => array(
"stud_detail" => array(
"name" => "Diana Green",
"email" => "[email protected]"),
"subjects" => array(
"Math" => 75,
"Science" => 80,
"English" => 90)));
foreach ($allStudents as $studentKey => $student) {
echo "<h3>$studentKey</h3>";
echo "<p><strong>Name:</strong> " . $student['stud_detail']['name'] . "</p>";
echo "<p><strong>Email:</strong> " . $student['stud_detail']['email'] . "</p>";
echo "<h4>Subjects and Marks</h4>";
echo "<ul>";
foreach ($student['subjects'] as $subject => $marks) {
echo "<li><strong>$subject:</strong> $marks</li>";
}
echo "</ul>";
}
?>
</body>
</html>
Output:
13. Write a PHP script to demonstrate the use of following array functions:
a. array_diff()
b. array_merge()
c. array_intersect()
d. array_shift()
e. array_unshift()
f. array_flip()
Code:
<html>
<head>
<title>Array Functions Demonstration</title>
</head>
<body>
<?php
$array1 = array("a" => "apple", "b" => "banana", "c" => "cherry", "d" => "date");
$array2 = array("b" => "banana", "c" => "cherry", "e" => "elderberry", "f" => "fig");
$shiftedValue = array_shift($array1);
echo "<h3>array_shift()</h3>";
echo "<p>Shifted Value: $shiftedValue</p>";
echo "<pre>";
print_r($array1);
echo "</pre>";
array_unshift($array1, "apricot");
echo "<h3>array_unshift()</h3>";
echo "<pre>";
print_r($array1);
echo "</pre>";
$flipped = array_flip($array2);
echo "<h3>array_flip()</h3>";
echo "<pre>";
print_r($flipped);
echo "</pre>";
?>
</body>
</html>
Output:
1. 2. 3.
4. 5. 6.
14. Write a PHP script to print the current date in the following format:
a. 16.08.21 b. 16-08-21
Code:
<html>
<head>
<title>Current Date Formats</title>
</head>
<body>
<h2>Current Date in Different Formats</h2>
<?php
$currentDate = date("d.m.y");
$currentDateHyphen = date("d-m-y");
echo "<p><strong>Format a (dd.mm.yy):</strong> $currentDate</p>";
echo "<p><strong>Format b (dd-mm-yy):</strong> $currentDateHyphen</p>";
?>
</body>
</html>
Output:
Code 2:
<?php
if (isset($_GET['monthNumber'])) {
$monthNumber = intval($_GET['monthNumber']);
if ($monthNumber >= 1 && $monthNumber <= 12) {
$months = array(
1 => "January",
2 => "February",
3 => "March",
4 => "April",
5 => "May",
6 => "June",
7 => "July",
8 => "August",
9 => "September",
10 => "October",
11 => "November",
12 => "December"
);
$monthName = $months[$monthNumber];
echo "<p>The month name for month number $monthNumber is
<strong>$monthName</strong>.</p>";
} else {
echo "<p>Please enter a valid month number between 1 and 12.</p>";
}
}
?>
<html>
<head>
<title>Result</title>
</head>
<body>
<a href="15.php"></a>
</body>
</html>
Output:
16. Write a PHP script to print the date in this format: 16th of August 2021 11:14:45 AM
Code:
<html lang="en">
<head>
<title>Formatted Date</title>
</head>
<body>
<h2>Formatted Date</h2>
<?php
$day = date("jS");
$month = date("F");
$year = date("Y");
$time = date("h:i:s A");
$formattedDate = "$day of $month $year $time";
echo "<p>$formattedDate</p>";
?>
</body>
</html>
Output:
17. Write a PHP script to calculate and display the number of days between two dates.
Code 1:
<html>
<head>
<title>Days Between Dates</title>
</head>
<body>
<h2>Calculate Number of Days Between Two Dates</h2>
<form action="date.php" method="get">
<label for="startDate">Start Date:</label>
<input type="date" id="startDate" name="startDate" required>
<br><br>
<label for="endDate">End Date:</label>
<input type="date" id="endDate" name="endDate" required>
<br><br>
<input type="submit" value="Calculate">
</form>
</body>
</html>
Code 2:
<?php
if (isset($_GET['startDate']) && isset($_GET['endDate'])) {
$startDate = $_GET['startDate'];
$endDate = $_GET['endDate'];
$startDateTime = new DateTime($startDate);
$endDateTime = new DateTime($endDate);
$interval = $startDateTime->diff($endDateTime);
echo "<h3>Result</h3>";
echo "<p>The number of days between $startDate and $endDate is: <strong>" .
$interval->days . "</strong> days.</p>";
}
?>
<html>
<head>
<title>Result</title>
</head>
<body>
<a href="17.php"></a>
</body>
</html>
Output:
Code 2:
<?php
if (isset($_GET['birthDate'])) {
$birthDate = $_GET['birthDate'];
$birthDateTime = new DateTime($birthDate);
$currentDateTime = new DateTime();
$interval = $currentDateTime->diff($birthDateTime);
echo "<h3>Your Age</h3>";
echo "<p>You are <strong>" . $interval->y . "</strong> years, <strong>" . $interval->m .
"</strong> months, and <strong>" . $interval->d . "</strong> days old.</p>";
}
?>
<html>
<head>
<title>Result</title>
</head>
<body>
<a href="18.php"></a>
</body>
</html>
Output:
19. Create a HTML page to accept a value from the user and list of Maths functions to select
from. Apply the appropriate math function using php script and display formatted output to
the user.
Code 1:
<html>
<head>
<title>Math Functions</title>
</head>
<body>
<h2>Apply Math Function</h2>
<form action="math.php" method="get">
<label for="value">Enter a Numeric Value:</label>
<input type="number" step="any" id="value" name="value" required>
<br><br>
<label for="function">Select a Math Function:</label>
<select id="function" name="function" required>
<option value="sqrt">Square Root</option>
<option value="exp">Exponential</option>
<option value="log">Natural Logarithm</option>
<option value="sin">Sine</option>
<option value="cos">Cosine</option>
<option value="tan">Tangent</option>
<option value="abs">Absolute Value</option>
<option value="ceil">Ceiling</option>
<option value="floor">Floor</option>
<option value="round">Round</option>
</select>
<br><br>
<input type="submit" value="Apply Function">
</form>
</body>
</html>
Code 2:
<?php
if (isset($_GET['value']) && isset($_GET['function'])) {
$value = $_GET['value'];
$function = $_GET['function'];
$result = null;
switch ($function) {
case 'sqrt':
$result = sqrt($value);
break;
case 'exp':
$result = exp($value);
break;
case 'log':
$result = log($value);
break;
case 'sin':
$result = sin($value);
break;
case 'cos':
$result = cos($value);
break;
case 'tan':
$result = tan($value);
break;
case 'abs':
$result = abs($value);
break;
case 'ceil':
$result = ceil($value);
break;
case 'floor':
$result = floor($value);
break;
case 'round':
$result = round($value);
break;
default:
echo "<p>Invalid function selected.</p>";
exit();
}
echo "<h3>Result</h3>";
echo "<p>The result of applying the <strong>$function</strong> function to
<strong>$value</strong> is: <strong>$result</strong>.</p>";
}
?>
<html>
<head>
<title>Result</title>
</head>
<body>
<a href="19.php"></a>
</body>
</html>
Output:
1.
2.
3.
4.
5.
6.
7.
8.