KTU Web Programming QPs
KTU Web Programming QPs
Answer
1. Defining a Function: A function is defined using the `function` keyword, followed by the
function name, parentheses (which can include parameters), and a block of code.
<?php
function greet() {
echo "Hello, World!";
}
?>
2. Calling a Function: After defining the function, you can call it in your program using the
function name followed by parentheses.
<?php
greet(); // Output: Hello, World!
?>
3. Function with Parameters: You can pass parameters to a function to make it more flexible.
<?php
function greet($name) {
echo "Hello, " . $name . "!";
}
4. Function with Return Values: A function can return a value using the `return` keyword.
<?php
function add($a, $b) {
return $a + $b;
}
5. Default Parameters: You can define default parameter values in case no arguments are
provided when calling the function.
<?php
function greet($name = "Guest") {
echo "Hello, " . $name . "!";
}
6. Variable Scope: Variables inside a function have local scope, meaning they are not
accessible outside the function unless specified as global.
<?php
$globalVar = 10;
function showGlobalVar() {
global $globalVar; // Access the global variable
echo $globalVar;
}
showGlobalVar(); // Output: 10
?>
2) Distinguish between implode and explode function in php with suitable example.
IMPLODE EXPLODE
Convert an array of string to a single string, Explode a string into substring and store it in
separating the parts with a specified string array
Syntax: Syntax:
implode(separator,array); explode(delimiter,string);
Example: Example:
$array=[‘apple’,’banana’,’cherry’]; $array=”apple,banana,cherry”;
$string=implode(“,”,$array); $string=explode(“,”,$string);
echo $string; print_r($array);
Output:
Output: Array
apple,banana,cherry {
[0]->apple
[1]->banana
[2]->cherry
}
3) What is the significance of cookies in web ? How can a cookie be created and
destroyed in PHP ?
A) Cookies are small pieces of data that websites store on a user's browser. They are significant
for various reasons in web development:
Significance of Cookies in Web
1. Session Management: Cookies are commonly used to manage user sessions, allowing
users to stay logged in as they navigate across different pages of a website. This is
especially useful for e-commerce sites or any platform requiring user authentication.
2. User Preferences: Cookies help in remembering user preferences and settings, such as
language selection, theme choices, and layout settings, leading to a personalized user
experience on subsequent visits.
3. Tracking and Analytics: Websites use cookies to track user behavior, such as the
pages visited and the time spent on the site. This data can be invaluable for web
analytics and improving website performance.
4. Shopping Cart Management: In e-commerce, cookies can store items in a shopping
cart, allowing users to return later and find their selected products still available.
5. Targeted Advertising: Cookies are used in advertising to track user interactions with
ads and deliver targeted advertisements based on user interests and browsing history.
Creating Cookies
In PHP, cookies can be created using the setcookie() function. The basic syntax is:
Destroying Cookies
To delete a cookie, you can use the setcookie() function again but set its expiration date to a
time in the past:
<?php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Factorial Table</title>
<style>
table {
width: 50%;
border-collapse: collapse;
margin: 20px auto;
font-family: Arial, sans-serif;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: center;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<table>
<tr>
<th>Number</th>
<th>Factorial</th>
</tr>
<?php
// Function for calculating factorial
function factorial($n) {
if ($n == 0 || $n == 1) {
return 1;
} else {
return $n * factorial($n - 1);
}
}
// Displaying factorial of 1 to 10 in table
for ($i = 1; $i <= 10; $i++) {
echo "<tr><td>$i</td><td>" . factorial($i) . "</td></tr>";
}
?>
</table>
</body>
</html>
[Q] 3 (b). Design the HTML page which enters a given number and embed the PHP code to
display a message indicating, whether the number is odd or even, when clicking on the
"CHECK NUMBER" button.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Odd or Even Checker</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
input[type="number"] {
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 4px;
width: 100%;
}
button {
padding: 10px 20px;
background-color: #28a745;
border: none;
color: white;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #218838;
}
.result {
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>Odd or Even Number Checker</h1>
<form method="POST">
<input type="number" name="number" placeholder="Enter a number" required>
<br>
<button type="submit">CHECK NUMBER</button>
</form>
<div class="result">
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST['number'];
</body>
</html>
[Q] 17.b
i) Declare an associative array named "marks" to store the key-value pairs ("Ram", 40),
("Alice", 20),("Raj",45), ("Mary", 35).
ii) Modify the value associated with the key "Ram" to 50.
iii) Sort the array and print the sorted key value pairs.
$marks = [
"Ram" => 40,
"Alice" => 20,
"Raj" => 45,
"Mary" => 35
];
ii) Modify the value associated with the key "Ram" to 50.
$marks["Ram"] = 50;
iii) Sort the array and print the sorted key-value pairs
asort($marks);
foreach ($marks as $key => $value) {
echo "$key => $value\n";
}
<?php
// i) Declare the associative array
$marks = [
"Ram" => 40,
"Alice" => 20,
"Raj" => 45,
"Mary" => 35
];
?>
Q2.Create a valid html document for yourself, including your name,address, and e mail
address. Also add your college;your major and the course.perform form handling in php
and process the output using post method
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Personal Information Form</title>
</head>
<body>
<h1>Personal Information Form</h1>
<label for="address">Address:</label><br>
<input type="text" id="address" name="address" required><br><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br><br>
<label for="college">College:</label><br>
<input type="text" id="college" name="college" required><br><br>
<label for="major">Major:</label><br>
<input type="text" id="major" name="major" required><br><br>
<label for="course">Course:</label><br>
<input type="text" id="course" name="course" required><br><br>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Collect form data
$name = htmlspecialchars($_POST['name']);
$address = htmlspecialchars($_POST['address']);
$email = htmlspecialchars($_POST['email']);
$college = htmlspecialchars($_POST['college']);
$major = htmlspecialchars($_POST['major']);
$course = htmlspecialchars($_POST['course']);
17 a.Design the HTML page which enters two numbers and embed the PHP code to
display the sum, difference, product and quotient of these two numbers, when clicking
the 'CALCULATE' button?
Answer:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Calculator</title>
</head>
<body>
<h1>Simple Calculator</h1>
<form method="post">
<label for="number1">Number 1:</label>
<input type="number" name="number1" required>
<br>
<label for="number2">Number 2:</label>
<input type="number" name="number2" required>
<br>
<input type="submit" name="calculate" value="CALCULATE">
</form>
<?php
if (isset($_POST['calculate'])) {
$number1 = $_POST['number1'];
$number2 = $_POST['number2'];
echo "<h2>Results:</h2>";
echo "Sum: $sum <br>";
echo "Difference: $difference <br>";
echo "Product: $product <br>";
echo "Quotient: $quotient <br>";
}
?>
</body>
</html>
17.(b) What are the uses of cookies in web pages? Describe syntax for setting cookies in
PHP. How can you access and delete the cookie using setcookie() function?
Cookies are often used to identify users and are small files embedded on the user's device by
the server. Whenever the same computer requests a page, the browser sends the cookie along
with the request. In PHP, cookies can be both created and retrieved.
Uses Of Cookies
Session Management: Cookies help websites maintain a user's login state or remember user
preferences (like themes, language, etc.) across different visits.
Personalization: Websites use cookies to provide personalized content based on the user's
past browsing behavior or preferences.
Tracking: Cookies can track user behavior across different sessions, providing analytics about
site usage, visits, and marketing effectiveness.
Security: Cookies can store session tokens to ensure users are authenticated and prevent
CSRF (Cross-Site Request Forgery) attacks.
Deleting a Cookie
setcookie("user", "", time() - 3600, "/");