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

KTU Web Programming QPs

The document provides a comprehensive overview of using functions in PHP, including defining, calling, and using parameters, as well as explaining the differences between implode and explode functions. It also discusses the significance of cookies in web development, how to create and destroy them in PHP, and includes examples of PHP scripts for calculating factorials, checking odd/even numbers, handling personal information forms, and performing basic arithmetic operations. Additionally, it covers the use of associative arrays and their manipulation in PHP.

Uploaded by

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

KTU Web Programming QPs

The document provides a comprehensive overview of using functions in PHP, including defining, calling, and using parameters, as well as explaining the differences between implode and explode functions. It also discusses the significance of cookies in web development, how to create and destroy them in PHP, and includes examples of PHP scripts for calculating factorials, checking odd/even numbers, handling personal information forms, and performing basic arithmetic operations. Additionally, it covers the use of associative arrays and their manipulation in PHP.

Uploaded by

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

GROUP 3

Q1.How to use functions in php programs with suitable examples

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 . "!";
}

greet("John"); // Output: Hello, John!


?>

4. Function with Return Values: A function can return a value using the `return` keyword.

<?php
function add($a, $b) {
return $a + $b;
}

$result = add(5, 3);


echo $result; // Output: 8
?>

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 . "!";
}

greet(); // Output: Hello, Guest!


greet("Alice"); // Output: Hello, Alice!
?>

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

The input type is an array The input type is a string

The output type is a string The output type is an 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 and Destroying Cookies in PHP

Creating Cookies

In PHP, cookies can be created using the setcookie() function. The basic syntax is:

setcookie(name, value, expire, path, domain, secure, httponly);

Example of Creating a Cookie


<?php
// Set a cookie named "user" with the value "John Doe" that expires in
1 hour
setcookie("user", "John Doe", time() + 3600, "/"); // Path "/" makes
it available site-wide
?>

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

// Delete the cookie named "user"


setcookie("user", "", time() - 3600, "/"); // Setting an expiration
time in the past
?>
[Q] 3. Write an embedded PHP script which displays the factorial of all numbers from 1 to
10 in a table in the web page. The factorial should be calculated and returned from a
function. The table headings should be "Number" and "Factorial"

<!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'];

// Check if the number is even or odd


if ($number % 2 == 0) {
echo "<p>The number <strong>$number</strong> is <strong>Even</strong>.</p>";
} else {
echo "<p>The number <strong>$number</strong> is <strong>Odd</strong>.</p>";
}
}
?>
</div>
</div>

</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.

iv)The entry identified by the key "Raj"


Ans)
i) Declare an associative array named "marks" to store the key-value pairs ("Ram", 40), ("Alice",
20), ("Raj", 45), ("Mary", 35).

$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";
}

iv) Refer to the entry identified by the key "Raj".

echo "Raj's mark is: " . $marks["Raj"];

Final PHP Code-

<?php
// i) Declare the associative array
$marks = [
"Ram" => 40,
"Alice" => 20,
"Raj" => 45,
"Mary" => 35
];

// ii) Modify the value associated with the key "Ram"


$marks["Ram"] = 50;

// iii) Sort the array and print sorted key-value pairs


asort($marks);
foreach ($marks as $key => $value) {
echo "$key => $value\n";
}
// iv) Refer to the entry identified by the key "Raj"
echo "Raj's mark is: " . $marks["Raj"];

?>

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

HTML Form (index.html)

<!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>

<form action="process.php" method="POST">


<label for="name">Name:</label><br>
<input type="text" id="name" name="name" required><br><br>

<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>

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


</form>
</body>
</html>

PHP Script for Handling

<?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']);

// Display the submitted information


echo "<h1>Submitted Information</h1>";
echo "<p><strong>Name:</strong> $name</p>";
echo "<p><strong>Address:</strong> $address</p>";
echo "<p><strong>Email:</strong> $email</p>";
echo "<p><strong>College:</strong> $college</p>";
echo "<p><strong>Major:</strong> $major</p>";
echo "<p><strong>Course:</strong> $course</p>";
} else {
echo "Invalid request method.";
}
?>

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'];

$sum = $number1 + $number2;


$difference = $number1 - $number2;
$product = $number1 * $number2;
$quotient = $number2 != 0 ? $number1 / $number2 : 'undefined';

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.

Setting Cookies in PHP


setcookie(name, value, expire, path, domain, secure, httponly);

Accessing Cookies in PHP


if(isset($_COOKIE['user'])) {
echo "User is: " . $_COOKIE['user'];
} else {
echo "Cookie is not set.";
}

Deleting a Cookie
setcookie("user", "", time() - 3600, "/");

You might also like