Assignment PHP 1
Assignment PHP 1
ASSIGNMENT :02
Input:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Factorial Calculator</title>
</head>
<body>
<h1>Factorial Calculator</h1>
</form>
<?php
function factorial($n) {
if ($n == 0 || $n == 1) {
return 1;
} else {
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST['number'];
if ($number >= 0) {
$result = factorial($number);
} else {
?>
</body>
</html>
OUTPUT:
2. Passing by Value and Reference: Create a PHP script
that demonstrates passing variables by value and by
reference in functions. Display how modifying a
variable within the function behaves differently in each
case?
CODE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Passing by Value and Reference</title>
</head>
<body>
<h1>Passing by Value and Reference in PHP</h1>
<?php
// Function that modifies the variable (passing by value)
function modifyByValue($num) {
$num += 10;
echo "<p>Inside modifyByValue: \$num is $num</p>";
}
// Function that modifies the variable (passing by reference)
function modifyByReference(&$num) {
$num += 10;
echo "<p>Inside modifyByReference: \$num is $num</p>";
}
</body>
</html>
OUTPUT:
3. Working with Forms (GET and POST Methods): Design
a login form that collects a username and password.
Validate the inputs using PHP and display a success or
error message based on the user's input using the POST
method?
CODE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Login Form</title>
</head>
<body>
<h2>Login Form</h2>
<!-- Login Form -->
<form method="POST" action="">
<label for="username">Username:</label>
<input type="text" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" name="password" required><br><br>
<?php
// Predefined username and password (For demo purposes)
$validUsername = "user123";
$validPassword = "password123";
Output:
4. HTML Form Controls and PHP Integration: Develop a form
that takes user feedback (name, email, rating, comments) and
stores the input in an array. Then display the collected feedback
on a separate page using the GET method?
CODE:
<!-- feedback_form.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>User Feedback Form</title>
</head>
<body>
<h2>User Feedback Form</h2>
<label for="email">Email:</label>
<input type="email" name="email"><br><br>
<label for="rating">Rating (1-5):</label>
<select name="rating" >
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select><br><br>
<label for="comments">Comments:</label><br>
<textarea name="comments" rows="5" cols="30"
required></textarea><br><br>
CODE:
<!-- display_feedback.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Display Feedback</title>
</head>
<body>
<h2>Collected User Feedback</h2>
<?php
// Check if all data is present
if (isset($_GET['name']) && isset($_GET['email']) &&
isset($_GET['rating']) && isset($_GET['comments'])) {
// Retrieve data and sanitize
$feedback = [
'name' => htmlspecialchars($_GET['name']),
'email' => htmlspecialchars($_GET['email']),
'rating' => htmlspecialchars($_GET['rating']),
'comments' => htmlspecialchars($_GET['comments'])
];
CODE:
<!-- language_selection.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Select Your Preferred Language</title>
</head>
<body>
<?php
// Check if the user has already set a preferred language cookie
if (isset($_COOKIE['language'])) {
// Retrieve the language from the cookie
$language = $_COOKIE['language'];
<?php
// Set the cookie if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST" &&
isset($_POST['language'])) {
// Set the cookie for 30 days
$selectedLanguage = $_POST['language'];
setcookie('language', $selectedLanguage, time() + (30 * 24 * 60 * 60));
// Cookie valid for 30 days
</body>
</html>
OUTPUT:
6. Sessions: Write a PHP script to implement a shopping cart
system. Use sessions to add and remove items from the cart, and
display the cart's content on a separate page?
CODE:
<h2>Shopping Page</h2>
</body>
</html>
// Initialize message
$message = '';
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Shopping Cart</title>
</head>
<body>
</body>
</html>
CODE:
<!-- login.php -->
<?php
session_start(); // Start a session
// Simple authentication
if ($username === $valid_username && $password ===
$valid_password) {
$_SESSION['username'] = $username; // Store username in
session
header("Location: welcome.php"); // Redirect to welcome page
exit();
} else {
$error_message = "Invalid username or password.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Login Page</title>
</head>
<body>
<h2>Login</h2>
<?php if (isset($error_message)) echo "<p
style='color:red;'>$error_message</p>"; ?>
<label for="password">Password:</label>
<input type="password" name="password" required><br><br>
</body>
</html>
// Handle logout
if (isset($_GET['logout'])) {
session_destroy(); // Destroy the session
header("Location: login.php"); // Redirect to login page
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome Page</title>
</head>
<body>
</body>
</html>