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

web

The document contains three PHP scripts. The first script performs basic arithmetic operations on two variables, the second script creates an HTML form for user input with validation, and the third script displays the current date and time while showcasing student marks in a table with conditional formatting. Each script is designed to demonstrate different PHP functionalities and user interactions.

Uploaded by

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

web

The document contains three PHP scripts. The first script performs basic arithmetic operations on two variables, the second script creates an HTML form for user input with validation, and the third script displays the current date and time while showcasing student marks in a table with conditional formatting. Each script is designed to demonstrate different PHP functionalities and user interactions.

Uploaded by

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

Q1.

Write a PHP script to calculate and display the result of


adding, subtracting, multiplying, and dividing two variables
($num1 = 20, $num2 = 10).

code:-
<?php
$num1 = 20;
$num2 = 10;

//Addition
$add = $num1 + $num2;
echo "Adiition of $num1 and $num2 is: $add <br>";

//Subtraction
$sub = $num1 - $num2;
echo "Subtraction of $num1 and $num2 is: $sub <br>";

//Multiplication
$pro = $num1 * $num2;
echo "Multiplication of $num1 and $num2 is: $pro <br>";

//Division
$division = $num1 / $num2;
echo "Division of $num1 and $num2 is: $division <br>";
?>

Q2. Create an HTML form with fields for Name, Email, and
Age. Write a PHP script to: (03)
• Validate the input to ensure all fields are filled.
• Display an error message if validation fails.
• Show the entered data on the same page if validation is
successful.

code:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Validation</title>
</head>
<body>
<?php
$name = $email = $age = "";
$nameErr = $emailErr = $ageErr = "";
$isValid = true;

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// Validate Name
if (empty($_POST["name"]))
{
$nameErr = "Name is required";
$isValid = false;
}
else
{
$name = htmlspecialchars($_POST["name"]);
}
// Validate Email
if (empty($_POST["email"]))
{
$emailErr = "Email is required";
$isValid = false;
}
else
{
$email = htmlspecialchars($_POST["email"]);
}

// Validate Age
if (empty($_POST["age"]))
{
$ageErr = "Age is required";
$isValid = false;
}
else
{
$age = htmlspecialchars($_POST["age"]);
}
}
?>

<h1>Input Form</h1>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" value="<?php echo $name; ?>">
<span style="color: red;">* <?php echo $nameErr; ?></span><br><br>

<label for="email">Email:</label><br>
<input type="email" id="email" name="email" value="<?php echo $email; ?>">
<span style="color: red;">* <?php echo $emailErr; ?></span><br><br>

<label for="age">Age:</label><br>
<input type="number" id="age" name="age" value="<?php echo $age; ?>">
<span style="color: red;">* <?php echo $ageErr; ?></span><br><br>

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


</form>
<?php
// If all fields are valid, display the entered data
if ($isValid)
{
echo "<h2>Submitted Data:</h2>";
echo "<p><strong>Name:</strong> $name</p>";
echo "<p><strong>Email:</strong> $email</p>";
echo "<p><strong>Age:</strong> $age</p>";
}
?>
</body>
</html>

Q3. Create a dynamic PHP webpage that: (03)


Displays the current date and time in the format:
Today is: Wednesday, December 25, 2024, Time: 4:30 PM.
Uses an associative array to store the names and marks of 5
students. Display this data in an HTML
Table and highlight rows where marks are greater than 80.

code:-
<?php
// Set the default timezone
date_default_timezone_set('America/New_York'); // Change to your timezone

// Get the current date and time


$currentDateTime = date('l, F j, Y, \T\i\m\e: g:i A');

// Associative array of students and their marks


$students = [
"Rida" => 98,
"Fatima" => 89,
"Zarwa" => 79,
"Malaika" => 85,
"Esha" => 67,
"Nazish" => 86,
"Khansa" => 79
];
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student Marks</title>
<style>
table {
width: 50%;
border-collapse: collapse;
margin: 20px 0;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
.highlight {
background-color:rgb(216, 240, 236); /* Light green background for high
marks */
}
</style>
</head>
<body>
<h1>Current Date and Time</h1>
<p>Today is: <?php echo $currentDateTime; ?></p>

<h2>Student Marks</h2>
<table>
<tr>
<th>Name</th>
<th>Marks</th>
</tr>
<?php foreach ($students as $name => $marks): ?>
<tr class="<?php echo ($marks > 80) ? 'highlight' : ''; ?>">
<td><?php echo htmlspecialchars($name); ?></td>
<td><?php echo htmlspecialchars($marks); ?></td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy