0% found this document useful (0 votes)
15 views14 pages

WT 4mq

The document provides a comprehensive overview of various HTML and PHP functionalities, including form creation, array manipulation, email sending, variable scope, and file handling. It includes code examples for each topic, demonstrating how to implement these features in web development. Additionally, it discusses advantages and disadvantages of protocols like IMAP4 and features of PHP.

Uploaded by

soham12306
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)
15 views14 pages

WT 4mq

The document provides a comprehensive overview of various HTML and PHP functionalities, including form creation, array manipulation, email sending, variable scope, and file handling. It includes code examples for each topic, demonstrating how to implement these features in web development. Additionally, it discusses advantages and disadvantages of protocols like IMAP4 and features of PHP.

Uploaded by

soham12306
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/ 14

‭1.

Design HTML Form to Accept User Input‬


‭HTML Code:‬

‭ tml‬
h
‭Copy code‬
‭<!DOCTYPE html>‬
‭<html lang="en">‬
‭<head>‬
‭<meta charset="UTF-8">‬
‭<meta name="viewport" content="width=device-width, initial-scale=1.0">‬
‭<title>User Input Form</title>‬
‭</head>‬
‭<body>‬
‭<h2>User Information Form</h2>‬
‭<form action="submit_form.php" method="post">‬
‭<label for="fname">First Name:</label><br>‬
‭<input type="text" id="fname" name="fname" required><br><br>‬

‭ label for="mname">Middle Name:</label><br>‬


<
‭<input type="text" id="mname" name="mname"><br><br>‬

‭ label for="lname">Last Name:</label><br>‬


<
‭<input type="text" id="lname" name="lname" required><br><br>‬

‭ label for="address">Address:</label><br>‬
<
‭<textarea id="address" name="address" rows="4" cols="50"></textarea><br><br>‬

‭ label for="contact">Contact Number:</label><br>‬


<
‭<input type="text" id="contact" name="contact" required><br><br>‬

‭ button type="submit">Submit</button>‬
<
‭<button type="reset">Reset</button>‬
‭</form>‬
‭</body>‬
‭</html>‬

‭2. Explain PHP Functions:‬


i‭ ) array_intersect()‬
‭This function compares two or more arrays and returns an array with the values that exist in‬
‭all the arrays.‬

‭Syntax:‬

‭ hp‬
p
‭Copy code‬
‭array_intersect(array1, array2, array3, ...);‬

‭Example:‬

‭ hp‬
p
‭Copy code‬
‭$array1 = [1, 2, 3, 4];‬
‭$array2 = [2, 3, 5];‬
‭$result = array_intersect($array1, $array2);‬
‭// Output: [2, 3]‬

i‭ i) array_slice()‬
‭This function extracts a slice of an array. It allows us to extract a portion of an array without‬
‭modifying the original array.‬

‭Syntax:‬

‭ hp‬
p
‭Copy code‬
‭array_slice(array, offset, length, preserve_keys);‬

‭Example:‬

‭ hp‬
p
‭Copy code‬
‭$array = [1, 2, 3, 4, 5];‬
‭$result = array_slice($array, 1, 3);‬
‭// Output: [2, 3, 4]‬

‭3. How to Send Email with PHP‬


‭PHP has a built-in function‬‭mail()‬‭that allows you to send emails.‬

‭Syntax:‬

‭ hp‬
p
‭Copy code‬
‭mail(to, subject, message, headers, parameters);‬

‭Example:‬

‭ hp‬
p
‭Copy code‬
‭$to = "[email protected]";‬
‭ subject = "Test Email";‬
$
‭$message = "This is a test email using PHP.";‬
‭$headers = "From: [email protected]";‬

‭if(mail($to, $subject, $message, $headers)) {‬


‭echo "Email sent successfully!";‬
‭} else {‬
‭echo "Failed to send email.";‬
‭}‬

‭4. Different Types of Arguments Passing to a Function‬


‭ HP supports two main ways of passing arguments to functions:‬‭pass by value‬‭and‬‭pass by‬
P
‭reference‬‭.‬

i‭ ) Pass by Value:‬‭When passing by value, the function gets a copy of the original value, so any‬
‭changes inside the function do not affect the original variable.‬

‭Example:‬

‭ hp‬
p
‭Copy code‬
‭function incrementValue($value) {‬
‭$value++;‬
‭}‬
‭$number = 5;‬
‭incrementValue($number);‬
‭echo $number; // Output: 5‬

i‭ i) Pass by Reference:‬‭In pass by reference, the function gets a reference to the original‬
‭variable, and changes to the parameter also affect the original variable.‬

‭Example:‬

‭ hp‬
p
‭Copy code‬
‭function incrementReference(&$value) {‬
‭$value++;‬
‭}‬
‭$number = 5;‬
‭incrementReference($number);‬
‭echo $number; // Output: 6‬

‭5. PHP Script to Count Occurrences in a File‬


‭php‬
‭ opy code‬
C
‭<?php‬
‭$inputFile = 'abc.txt';‬
‭$outputFile = 'abccount.txt';‬

‭ content = file_get_contents($inputFile);‬
$
‭$characters = count_chars($content, 1);‬

‭ output = "";‬
$
‭foreach ($characters as $char => $count) {‬
‭$output .= chr($char) . ": " . $count . "\n";‬
‭}‬

f‭ ile_put_contents($outputFile, $output);‬
‭echo "Character count written to abccount.txt";‬
‭?>‬

‭6. Advantages and Disadvantages of IMAP4 Protocol‬


‭Advantages:‬

‭●‬ M ‭ ail Synchronization:‬‭IMAP allows you to access your emails from multiple devices,‬
‭keeping them synchronized across platforms.‬
‭●‬ ‭Server Storage:‬‭Emails are stored on the server, so they don’t consume local storage.‬
‭●‬ ‭Search:‬‭You can search through your emails directly on the server without‬
‭downloading them.‬

‭Disadvantages:‬

‭●‬ S ‭ erver Space:‬‭Since emails are stored on the server, large inboxes can fill up server‬
‭space quickly.‬
‭●‬ ‭Internet Dependency:‬‭Full access to email content requires an internet connection,‬
‭as messages are not stored locally by default.‬

‭7. Scope of Variables in PHP‬


I‭ n PHP, variable scope determines where a variable can be accessed or modified. There are‬
‭four main types of scope in PHP:‬

i‭ ) Local Scope:‬
‭Variables declared inside a function have local scope, meaning they can only be accessed‬
‭within that function.‬

‭Example:‬

‭ hp‬
p
‭Copy code‬
‭function myFunction() {‬
‭ x = 10; // Local variable‬
$
‭echo $x;‬
}‭ ‬
‭myFunction(); // Output: 10‬
‭echo $x; // Error: Undefined variable‬

i‭ i) Global Scope:‬
‭Variables declared outside any function have global scope and can be accessed from‬
‭anywhere outside functions.‬

‭Example:‬

‭ hp‬
p
‭Copy code‬
‭$x = 20; // Global variable‬
‭function myFunction() {‬
‭global $x;‬
‭echo $x; // Output: 20‬
‭}‬
‭myFunction();‬

i‭ ii) Static Scope:‬


‭Static variables retain their value even after the function execution is completed. The value‬
‭of a static variable persists between function calls.‬

‭Example:‬

‭ hp‬
p
‭Copy code‬
‭function counter() {‬
‭static $count = 0;‬
‭$count++;‬
‭echo $count;‬
‭}‬
‭counter(); // Output: 1‬
‭counter(); // Output: 2‬

i‭ v) Superglobal:‬
‭Superglobals are built-in variables that are accessible from anywhere in the script.‬
‭Examples are‬‭$_GET‬‭,‬‭$_POST‬‭,‬‭$_SESSION‬‭, etc.‬

‭8.‬‭prepare()‬‭and‬‭execute()‬‭in Database Handling‬


‭ hen working with databases in PHP,‬‭prepare()‬‭and‬‭execute()‬‭are used to execute SQL‬
W
‭queries safely, especially when dealing with user inputs.‬
i‭ )‬‭prepare()‬‭:‬
‭This function prepares an SQL statement for execution. It prevents SQL injection by‬
‭escaping special characters in the input.‬

‭Syntax:‬

‭ hp‬
p
‭Copy code‬
‭$stmt = $conn->prepare("INSERT INTO users (name, email) VALUES (?, ?)");‬

i‭ i)‬‭execute()‬‭:‬
‭Once the SQL statement is prepared,‬‭execute()‬‭is called to execute the statement.‬

‭Syntax:‬

‭ hp‬
p
‭Copy code‬
‭$stmt->execute([$name, $email]);‬

‭Example:‬

‭ hp‬
p
‭Copy code‬
‭$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');‬
‭$stmt = $pdo->prepare("INSERT INTO users (name, email) VALUES (?, ?)");‬
‭$name = "John Doe";‬
‭$email = "[email protected]";‬
‭$stmt->execute([$name, $email]);‬

‭9. Functions for Reading and Writing Characters in Files‬


‭i) Reading Characters:‬

‭●‬ ‭fgetc()‬‭: This function reads a single character from a file.‬

‭Example:‬

‭ hp‬
p
‭Copy code‬
‭$file = fopen("example.txt", "r");‬
‭while (!feof($file)) {‬
‭$char = fgetc($file);‬
‭echo $char;‬
‭}‬
‭fclose($file);‬
‭ii) Writing Characters:‬

‭●‬ ‭fwrite()‬‭: This function writes characters to a file.‬

‭Example:‬

‭ hp‬
p
‭Copy code‬
‭$file = fopen("example.txt", "w");‬
‭fwrite($file, "Hello, World!");‬
‭fclose($file);‬

‭10. HTML Form for Accepting Username and Address‬


‭HTML Code:‬

‭ tml‬
h
‭Copy code‬
‭<!DOCTYPE html>‬
‭<html lang="en">‬
‭<head>‬
‭<meta charset="UTF-8">‬
‭<meta name="viewport" content="width=device-width, initial-scale=1.0">‬
‭<title>User Form</title>‬
‭</head>‬
‭<body>‬
‭<h2>User Input Form</h2>‬
‭<form action="submit_form.php" method="post">‬
‭<label for="username">Username:</label><br>‬
‭<input type="text" id="username" name="username" required><br><br>‬

‭ label for="address">Address:</label><br>‬
<
‭<textarea id="address" name="address" rows="4" cols="50"></textarea><br><br>‬

‭ button type="submit">Submit</button>‬
<
‭<button type="reset">Reset</button>‬
‭</form>‬
‭</body>‬
‭</html>‬

‭11. PHP Script to Sort an Associative Array in Descending Order‬


‭ hp‬
p
‭Copy code‬
‭<?php‬
‭$associativeArray = [‬
‭"John" => 80,‬
"‭ Alice" => 90,‬
‭"Mark" => 85,‬
‭];‬

‭arsort($associativeArray); // Sort in descending order‬

‭foreach ($associativeArray as $key => $value) {‬


‭echo "$key: $value<br>";‬
‭}‬
‭?>‬

‭12. PHP Program to Change Directory and Count Files/Directories‬


‭ hp‬
p
‭Copy code‬
‭<?php‬
‭$dir = readline("Enter directory name: ");‬

‭if (chdir($dir)) {‬
‭$files = scandir(getcwd());‬
‭$fileCount = 0;‬
‭$dirCount = 0;‬

‭foreach ($files as $file) {‬


‭if (is_file($file)) {‬
‭$fileCount++;‬
‭} elseif (is_dir($file)) {‬
‭$dirCount++;‬
‭}‬
‭}‬

‭ cho "Number of files: $fileCount\n";‬


e
‭echo "Number of directories: $dirCount\n";‬
‭} else {‬
‭echo "Directory not found.";‬
‭}‬
‭?>‬

‭13. Features of PHP‬


‭ .‬ O
1 ‭ pen Source‬‭: PHP is free to download and use.‬
‭2.‬ ‭Cross-platform Compatibility‬‭: PHP works on various platforms, such as Windows,‬
‭Linux, macOS.‬
‭3.‬ ‭Server-Side Scripting‬‭: PHP is used primarily for server-side scripting.‬
‭4.‬ ‭Database Integration‬‭: PHP supports databases like MySQL, PostgreSQL, SQLite.‬
‭5.‬ ‭Simplicity‬‭: PHP has an easy learning curve with a straightforward syntax.‬
‭14. Two Control Statements in PHP‬
‭i)‬‭if‬‭Statement:‬

‭ hp‬
p
‭Copy code‬
‭if (condition) {‬
‭// Code to execute if condition is true‬
‭}‬

‭Example:‬

‭ hp‬
p
‭Copy code‬
‭$age = 20;‬
‭if ($age >= 18) {‬
‭echo "You are an adult.";‬
‭}‬

‭ii)‬‭for‬‭Loop:‬

‭ hp‬
p
‭Copy code‬
‭for (initialization; condition; increment) {‬
‭// Code to execute‬
‭}‬

‭Example:‬

‭ hp‬
p
‭Copy code‬
‭for ($i = 0; $i < 5; $i++) {‬
‭echo $i;‬
‭}‬

‭15.‬‭explode()‬‭and‬‭implode()‬‭Functions‬
i‭ )‬‭explode()‬‭:‬
‭This function splits a string into an array based on a delimiter.‬

‭Syntax:‬

‭ hp‬
p
‭Copy code‬
‭explode(delimiter, string, limit);‬
‭Example:‬

‭ hp‬
p
‭Copy code‬
‭$string = "apple,banana,orange";‬
‭$array = explode(",", $string);‬
‭// Output: ["apple", "banana", "orange"]‬

i‭ i)‬‭implode()‬‭:‬
‭This function joins array elements into a string, with a specified delimiter.‬

‭Syntax:‬

‭ hp‬
p
‭Copy code‬
‭implode(separator, array);‬

‭Example:‬

‭ hp‬
p
‭Copy code‬
‭$array = ["apple", "banana", "orange"];‬
‭$string = implode("-", $array);‬
‭// Output: "apple-banana-orange"‬

‭16. PHP Program to Check if a Number is Palindrome‬


‭ hp‬
p
‭Copy code‬
‭<?php‬
‭$number = readline("Enter a number: ");‬
‭$reverse = strrev($number);‬

‭if ($number == $reverse) {‬


‭echo "$number is a Palindrome.";‬
‭} else {‬
‭echo "$number is not a Palindrome.";‬
‭}‬
‭?>‬

‭17. PHP Program to Sort Array Based on Marks‬


‭ hp‬
p
‭Copy code‬
‭ ?php‬
<
‭$students = [‬
‭["name" => "John", "marks" => 85],‬
‭["name" => "Alice", "marks" => 95],‬
‭["name" => "Bob", "marks" => 75],‬
‭];‬

‭usort($students, function ($a, $b) {‬


‭return $b['marks'] - $a['marks']; // Sort in descending order‬
‭});‬

‭foreach ($students as $student) {‬


‭echo $student['name'] . ": " . $student['marks'] . "<br>";‬
‭}‬
‭?>‬

‭18. PHP Script to Display Movies Released in 2010‬


‭ hp‬
p
‭Copy code‬
‭<?php‬
‭$pdo = new PDO('mysql:host=localhost;dbname=movieDB', 'username', 'password');‬
‭$query = $pdo->prepare("SELECT Movie_name FROM Movie WHERE ryear = 2010");‬
‭$query->execute();‬

‭ movies = $query->fetchAll(PDO::FETCH_ASSOC);‬
$
‭foreach ($movies as $movie) {‬
‭echo $movie['Movie_name'] . "<br>";‬
‭}‬
‭?>‬

‭19. Two Syntax Examples of‬‭foreach‬‭Loop‬


‭i) Looping Through Indexed Array:‬

‭ hp‬
p
‭Copy code‬
‭$numbers = [1, 2, 3, 4];‬
‭foreach ($numbers as $value) {‬
‭echo $value . "<br>";‬
‭}‬

‭ii) Looping Through Associative Array:‬

‭ hp‬
p
‭Copy code‬
‭ students = ["John" => 85, "Alice" => 90];‬
$
‭foreach ($students as $name => $marks) {‬
‭echo "$name: $marks<br>";‬
‭}‬

‭20. Types of Strings in PHP‬


‭1.‬ ‭Single-Quoted Strings:‬
‭○‬ ‭Literal strings without variable interpolation.‬
‭○‬ ‭Example:‬‭'Hello, World!'‬
‭2.‬ ‭Double-Quoted Strings:‬
‭○‬ ‭Support variable interpolation and special character sequences.‬
‭○‬ ‭Example:‬‭"Hello, $name"‬

‭21. Getting Array of Keys and Values from an Associative Array‬


‭ rray of Keys:‬
A
‭php‬
‭Copy code‬
‭$keys = array_keys($array);‬

‭●‬

‭ rray of Values:‬
A
‭php‬
‭Copy code‬
‭$values = array_values($array);‬

‭●‬

‭Example:‬

‭ hp‬
p
‭Copy code‬
‭$associativeArray = ["name" => "John", "age" => 25];‬
‭$keys = array_keys($associativeArray);‬
‭$values = array_values($associativeArray);‬

‭ rint_r($keys); // Output: ['name', 'age']‬


p
‭print_r($values); // Output: ['John', 25]‬

‭22. HTML Form for Employee Data‬


‭HTML Code:‬

‭ tml‬
h
‭Copy code‬
‭ !DOCTYPE html>‬
<
‭<html lang="en">‬
‭<head>‬
‭<meta charset="UTF-8">‬
‭<meta name="viewport" content="width=device-width, initial-scale=1.0">‬
‭<title>Employee Form</title>‬
‭</head>‬
‭<body>‬
‭<h2>Employee Information</h2>‬
‭<form action="submit_employee.php" method="post">‬
‭<label for="empnum">Employee Number:</label><br>‬
‭<input type="text" id="empnum" name="empnum" required><br><br>‬

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

‭ label for="age">Age:</label><br>‬
<
‭<input type="number" id="age" name="age" required><br><br>‬

‭ label for="gender">Gender:</label><br>‬
<
‭<input type="radio" id="male" name="gender" value="male" required> Male‬
‭<input type="radio" id="female" name="gender" value="female" required>‬
‭ emale<br><br>‬
F

‭ button type="submit">Submit</button>‬
<
‭<button type="reset">Reset</button>‬
‭</form>‬
‭</body>‬
‭</html>‬

‭23. PHP Script to Merge and Display Unique Values from Two Arrays‬
‭ hp‬
p
‭Copy code‬
‭<?php‬
‭$array1 = [1, 2, 3, 4];‬
‭$array2 = [3, 4, 5, 6];‬

‭$mergedArray = array_unique(array_merge($array1, $array2));‬

‭foreach ($mergedArray as $value) {‬


‭echo $value . "<br>";‬
‭}‬
‭?>‬

‭24. PHP Script to Display Student Data‬


‭ hp‬
p
‭Copy code‬
‭<?php‬
‭$pdo = new PDO('mysql:host=localhost;dbname=studentDB', 'username', 'password');‬
‭$query = $pdo->query("SELECT s.rollno, s.name, m.mark1, m.mark2, m.mark3, m.total,‬
‭m.per‬
‭FROM student s JOIN marks m ON s.rollno = m.rollno");‬

‭ cho "<table border='1'>‬


e
‭<tr>‬
‭<th>Roll No</th>‬
‭<th>Name</th>‬
‭<th>Mark 1</th>‬
‭<th>Mark 2</th>‬
‭<th>Mark 3</th>‬
‭<th>Total</th>‬
‭<th>Percentage</th>‬
‭</tr>";‬

‭while ($row = $query->fetch(PDO::FETCH_ASSOC)) {‬


‭echo "<tr>";‬
‭echo "<td>" . $row['rollno'] . "</td>";‬
‭echo "<td>" . $row['name'] . "</td>";‬
‭echo "<td>" . $row['mark1'] . "</td>";‬
‭echo "<td>" . $row['mark2'] . "</td>";‬
‭echo "<td>" . $row['mark3'] . "</td>";‬
‭echo "<td>" . $row['total'] . "</td>";‬
‭echo "<td>" . $row['per'] . "</td>";‬
‭echo "</tr>";‬
‭}‬

‭ cho "</table>";‬
e
‭?>‬

You might also like