Web Technologies I SEM 5
Web Technologies I SEM 5
(Sem 5)
a) What is hyperlink?
--> A Hyperlink is a clickable text or image on a web page that takes you to another
webpage or a specific section within the same page. It's essential for navigating the web.
1.Enhanced visual appeal: It allows for creative and visually appealing designs.
2.Improved page load speed: By separating styles from HTML, CSS reduces file size and
improves loading times.
3.Consistent formatting: It ensures consistent styling across multiple pages, saving time
and effort.
--> The <sup> tag is used to set text in superscript format. For example, x<sup>2</sup> will
display as x².
--> The pathinfo() function in PHP extracts information from a file path, returning details like
directory name, base name, filename without extension, and file extension.
--> HTTP is a stateless protocol, meaning it doesn't maintain session information between
requests. It uses a client-server model, where clients (browsers) request resources from
servers.
--> The foreach() function in PHP iterates over an array, executing a specified code block
for each element. It simplifies looping through arrays, making code more concise and
readable.
--> ftell(): Returns the current position of the file pointer, allowing you to track the
read/write location.
fseek(): Moves the file pointer to a specific position within the file, enabling random access
to any part of the file.
--> if (file_exists("file.txt")) {
unlink("file.txt");
} else {
This code checks if the file exists and then uses the unlink() function to delete it.
--> PHP has two compound data types: arrays and objects. Arrays store multiple values in
a single variable, while objects encapsulate properties and methods.
-->
define("CONSTANT_NAME", "value");
This defines a constant named CONSTANT_NAME with the value "value". Constants are
case-insensitive and cannot be redefined.
--> Lambda functions, also known as anonymous functions, are small, unnamed functions
defined without using the def keyword. They are often used for concise operations within
expressions or as arguments to higher-order functions.
e) Which function read the data from files with the assumption that it is
-->
$data = array();
$data[] = $row;
fclose($handle);
The fgetcsv() function is used to read CSV data from a file. It parses each line, splits it into
fields based on the delimiter, and returns an array containing the fields. The while loop
iterates over each line, adding the parsed data to the $data array.
f) Write a name of function that enables you to print the contents of a file
--> file_get_contents("file.txt");
The file_get_contents() function reads the entire contents of a file into a string. This string
can then be printed or further processed as needed. It's a convenient way to read files
without the explicit use of fopen().
--> POP3 (Post Office Protocol 3) is a protocol used to retrieve email messages from a mail
server. It allows users to download emails to their local device for offline access. POP3 is a
simple protocol that doesn't offer advanced features like real-time synchronization or
folder management.
-->
$flipped_array = array_flip($original_array);
The array_flip() function in PHP exchanges keys with their associated values in an array. It's
useful for reversing key-value pairs when needed.
j) Which function is use to reposition the file pointer to the beginning of the
file?
--> rewind($handle);
The rewind() function is used to reposition the file pointer to the beginning of a file opened
in read or write mode. This allows you to start reading or writing from the beginning again.
--> <style>
p{
color: blue;
font-size: 16px;
}
</style>
<p>This is a paragraph.</p>
The <style> tag is used to define styles for HTML elements. Styles defined within <style>
apply to the entire document. You can also use external stylesheets linked with the <link>
tag for better organization and reusability.
-->
3 It displays the outputs of one or more strings The print outputs only
. separated by commas. the strings.
-->
--> mkdir("new_directory");
This code creates a new directory named "new_directory" in the current working directory.
You can also specify a full path to create the directory in a specific location.
g) What is a DSN?
--> DSN (Data Source Name) is a string of characters that specifies the location of a
database. It typically includes information like the database server, database name,
username, and password. DSNs are used to connect applications to databases, making it
easier to manage database connections and switch between different databases.
--> POP3 (Post Office Protocol 3) and IMAP (Internet Message Access Protocol) are the two
primary protocols used to retrieve email from a mail server.
POP3 downloads emails to the user's local device, while IMAP allows users to access and
manage emails on the server, making them accessible from multiple devices.
-->
$object->name = "Alice";
$object->age = 30;
--> SMTP (Simple Mail Transfer Protocol) is a protocol used to send email messages
between servers. It defines the rules for how email messages are formatted and
transmitted. When you send an email, your email client uses SMTP to connect to a mail
server, which then forwards the email to the recipient's mail server using SMTP.
-->
Single-
Quoted Double-Quoted Strings
Strings
Syntax -: Syntax -:
2.
‘string’ “string”
-->
<!DOCTYPE html>
<html>
<head>
<title>External CSS</title>
</head>
<body>
<p>This is a paragraph.</p>
</body>
</html>
-To use external CSS, create a separate .css file (e.g., styles.css) and link it to your HTML
file using the <link> tag in the <head> section. The href attribute specifies the path to the
CSS file. The styles defined in the external CSS file will be applied to the HTML elements.
String?
$substring = "test";
} else {
The strpos() function is used to find the position of the first occurrence of a substring within
a string. It returns the position as an integer, or false if the substring is not found.
In the example above, the strpos() function searches for the substring "test" in the given
string and returns its position (10).
Q] What is the purpose of array_splice ( ) function?
-->
array_splice($fruits, 2, 2);
print_r($fruits); // Output: Array ( [0] => apple [1] => banana [3] => kiwi )
The array_splice() function is used to remove or replace elements from an array. It takes
three main arguments:
Optionally, you can provide additional arguments to insert new elements at the specified
offset. This function is powerful for manipulating arrays dynamically.
-->
PHP:
Server-Side Scripting: PHP code runs on the server before sending the generated HTML to
the user's browser. This allows for dynamic content creation and interaction.
Database Access: PHP can connect to databases like MySQL, enabling you to store and
retrieve data for web applications.
Form Handling: PHP can process data submitted through forms on web pages, allowing for
user interaction and data collection.
Session Management: PHP can track user sessions, maintaining information about a user
across multiple page requests.
Templating Engines: Popular PHP templating engines like Twig or Blade simplify the
process of separating logic from presentation by allowing you to build HTML structures
dynamically with PHP variables.
HTML:
Structure and Content: HTML provides the basic building blocks for web pages, defining the
structure and content using tags and attributes.
<?php
usort($age);
print_r($age);
?>
-->
Array (
[Julie] => 38
[Anna] => 45
[Benne] => 53
<?php
$a='PHP';
echo $b;
?>
-->
$a interpolation
$str=' abc,pqr,lmn,xyz';
$p=explode(',',$str,3);
print_r($p);
Array (
<?php
$p=array(1,2,3,4,5);
$q=array(1,3,5,7,9);
$s=array_diff($p,$q);
print_r($s);
?>
[1] => 2
[3] => 4
--> <ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<ol>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>
-->
$x = 10.5;
-Casting is the process of converting one data type to another. In PHP, you can use the
following syntax to cast variables:
-PHP
(type)expression
1.Explicit Casting: You manually specify the desired data type using parentheses.
2.Implicit Casting: PHP automatically performs type conversion in certain situations, such
as arithmetic operations with different data types.
Be aware of potential data loss or unexpected behavior when casting between different
data types.
<?php
print_r($subjects);
?>
i) var_dump()
--> The var_dump() function in PHP is used to print information about a variable,
including its data type and value. It's helpful for debugging purposes, especially when
working with complex data structures like arrays and objects.
Example:
$x = 10;
$y = "Hello, world!";
$z = array(1, 2, 3);
int(10)
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
ii) Trim ()
--> The trim() function removes whitespace from the beginning and end of a string. It
can be used to clean up user input or remove unnecessary whitespace from strings.
Example :
$trimmed_string = trim($string);
Hello, world!
The trim() function removes the leading and trailing whitespace from the original
string, resulting in the cleaned-up output.
These pages can change their content based on user input or server-side scripts.
They are typically created using technologies like PHP, JavaScript, and Python.
Examples include e-commerce websites, social media platforms, and web applications.
Dynamic web pages offer more interactivity and functionality compared to static pages.
They can adapt to user preferences, display personalized content, and process user input.
1. if-else Statement:
Syntax:
PHP
if (condition) {
} else {
Example:
PHP
$age = 18;
} else {
2. for Loop:
Syntax:
PHP
// code to be executed
Example:
i) explode (),
--> explode()
limit: Optional, specifies the maximum number of elements in the returned array.
ii) implode ()
--> implode()
a) Design HTML form that will accept user input as first name, middle
name and last name, address, contact number. Provide buttons to submit
<html>
<head>
</head>
<body>
<form>
</form>
</body>
</html>
Explanation:
1.HTML Structure:
The <head> tag contains metadata about the document, such as the title.
2.Form Elements:
3.Form Submission:
When the "Submit" button is clicked, the form data is sent to the server.
The server-side script (e.g., PHP, Python, or Node.js) can process the submitted data and
perform actions like storing it in a database or sending an email.
4.Form Reset:
The "Reset" button clears all the input fields in the form, allowing the user to start over.
i) array _ intersect ()
--> The array_intersect() function in PHP compares two or more arrays and returns an
array containing the values that are present in all of the input arrays.
iii) shuffle ()
--> The shuffle() function in PHP is used to randomly shuffle the elements of an array.
It rearranges the elements in a random order, modifying the original array.
--> <?php
$to = "[email protected]";
} else {
?>
To send an email using PHP, you typically use the mail() function:
1.Specify Recipients: Use the $to variable to set the recipient's email address.
2.Set Subject: Use the $subject variable to set the email's subject line.
3.Write Message: Use the $message variable to write the email's body.
4.Set Headers: Use the $headers variable to set additional headers, such as the sender's
email address.
5.Send Email: Call the mail() function with the specified parameters.
Note: To use the mail() function, you'll need a mail server configured on your server.
The specific configuration may vary depending on your hosting provider.
--> <?php
function myFunction() {
myFunction();
Global Scope: Variables declared outside any function have global scope and can be
accessed from anywhere within the script.
Local Scope: Variables declared inside a function have local scope and are only
accessible within that function.
In the example:
--> prepare():
Helps prevent SQL injection attacks by separating the query from the data.
execute():
c) Explain the functions used for reading and writing characters in files.
while (!feof($file)) {
$char = fgetc($file);
echo $char;
fclose($file);
fclose($file);
Reading Characters:
These functions provide basic file I/O operations in PHP. For more complex file handling,
you can use functions like fread(), fgets(), and file_put_contents().
--> <?php
function isPalindrome($number) {
$originalNumber = $number;
$reversedNumber = 0;
if (isPalindrome($num)) {
?>
b) Write a PHP program to sort array on marks (Array contains names and
marks).
--> <?php
$students = array(
);
usort($students, "cmp");
Write a PHP script which displays all movies name released in the year
2010.
--> <?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";
// Create connection
// Check connection
if ($conn->connect_error) {
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
} else {
echo "0 results";
$conn->close();
?>
1.Required Arguments:
2.Optional Arguments:
4.Passing by Reference:
-Understanding these types helps you create flexible and reusable functions.
b) Write a PHP script to read a file abc.txt where file contains character,
--> <?php
$fileContent = file_get_contents('abc.txt');
$charCounts = array();
$char = $fileContent[$i];
if (isset($charCounts[$char])) {
$charCounts[$char]++;
} else {
$charCounts[$char] = 1;
fclose($outputFile);
--> IMAP4 (Internet Message Access Protocol version 4) is a protocol used to access
and manage email messages on a mail server. It allows users to view, search, and
organize emails from multiple devices without needing to download them locally.
Advantages of IMAP4:
Remote Access: Users can access their emails from any device with an internet
connection.
Synchronization: Changes made to emails on one device are synchronized across all
devices.
Offline Access: Many email clients support offline access to emails, allowing users to
work with their emails even when not connected to the internet.
Search Capabilities: IMAP allows users to search for specific emails based on various
criteria, such as sender, recipient, subject, or keywords.
Disadvantages of IMAP4:
Higher Server Load: IMAP requires the server to store and manage all emails for all
users, which can increase server load and resource consumption.
Slower Performance: In some cases, IMAP can be slower than POP3, especially for
large mailboxes or slow internet connections.
Complex Configuration: IMAP can be more complex to configure than POP3,
especially for users who are not familiar with email server settings.
Security Risks: IMAP can be more vulnerable to security risks, such as unauthorized
access, if not properly configured and secured.
--> <?php
$arr = array("a" => 10, "b" => 20, "c" => 5, "d" => 15);
arsort($arr);
c) Accept directory name from user. Write PHP program to change current
directories in it.
-->
<?php
if (chdir($directory)) {
$files = 0;
$directories = 0;
$directories++;
} else {
$files++;
} else {
An HTTP request is a message sent from a client (usually a web browser) to a web
server. It consists of:
1.Method: Specifies the action to be performed (e.g., GET, POST, PUT, DELETE).
4.Headers: Provide additional information about the request, such as the client's
user-agent, content type, and cookies.
5.Body: Optional content included with the request, such as form data or file uploads.
HTTP Response:
1.Status Code: Indicates the status of the request (e.g., 200 OK, 404 Not Found, 500
Internal Server Error).
3.Headers: Provide additional information about the response, such as the content
type, content length, and server information.
4.Body: The actual content of the response, such as HTML, JSON, or image data.
Example:
HTTP
HTTP/1.1 200 OK
Content-Type: text/html
<!DOCTYPE html>
<html>
<head>
<title>Example Page</title>
</head>
<body>
<p>Hello, world!</p>
</body>
</html>
example.
If an optional parameter is not provided when calling the function, its default value is
used.
In the example, the $greeting parameter is optional with a default value of "Hello".
When calling greet("Alice"), only the $name argument is provided, so "Hello" is used
as the greeting.
--> <?php
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";
// Create connection
// Check connection
if ($conn->connect_error) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$sql = "INSERT INTO employees (name, email, phone) VALUES (?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->execute();
if ($stmt->affected_rows > 0) {
$stmt->close();
$conn->close();
?>
b) What is associative array? Explain with example how is it different from indexed
Array?
-->In PHP,
$associativeArray = array("fruit1" => "apple", "fruit2" => "banana", "fruit3" => "orange");
Indexed Arrays:
Associative Arrays:
Key Differences:
1.Indexing: Indexed arrays use numerical indices, while associative arrays use
custom keys.
2.Order: Indexed arrays maintain the order of elements, while associative arrays do
not.
--> In Php,
require_once 'DB.php';
$dsn = 'mysql://username:password@localhost/mydatabase';
$options = array(
'debug' => 2
);
if (DB::isError($db)) {
die($db->getMessage());
if (DB::isError($result)) {
die($result->getMessage());
print_r($row);
$db->disconnect();
PEAR DB Basics:
4.Fetching Results: Use fetchRow() to fetch rows from the result set.