WT 4mq
WT 4mq
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="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>
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]
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]";
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
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";
?>
● 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.
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();
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.
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]);
Example:
hp
p
Copy code
$file = fopen("example.txt", "r");
while (!feof($file)) {
$char = fgetc($file);
echo $char;
}
fclose($file);
ii) Writing Characters:
Example:
hp
p
Copy code
$file = fopen("example.txt", "w");
fwrite($file, "Hello, World!");
fclose($file);
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>
if (chdir($dir)) {
$files = scandir(getcwd());
$fileCount = 0;
$dirCount = 0;
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)forLoop:
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()andimplode()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"
movies = $query->fetchAll(PDO::FETCH_ASSOC);
$
foreach ($movies as $movie) {
echo $movie['Movie_name'] . "<br>";
}
?>
hp
p
Copy code
$numbers = [1, 2, 3, 4];
foreach ($numbers as $value) {
echo $value . "<br>";
}
hp
p
Copy code
students = ["John" => 85, "Alice" => 90];
$
foreach ($students as $name => $marks) {
echo "$name: $marks<br>";
}
●
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);
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];
cho "</table>";
e
?>