PHP Practical PDF
PHP Practical PDF
$a = 20;
$b = 6;
$sum = $a + $b;
$diff = $a - $b;
$prod = $a * $b;
$quot = $a / $b;
$rem = $a % $b;
$x = 10;
echo "Initial value of x: $x <br>"; // Output:
10
$x += 5;
echo "After x += 5: $x <br>"; // Output:
15
$x -= 3;
echo "After x -= 3: $x <br>"; // Output:
12
$x *= 2;
echo "After x *= 2: $x <br>"; // Output:
24
$x /= 4;
echo "After x /= 4: $x <br>"; // Output: 6
$x %= 3;
echo "After x %= 3: $x <br>"; // Output: 0
?>
3) Write a simple PHP program using expressions and using Comparison
operators.
<?php
// Using Comparison Operators
$a = 10;
$b = 20;
$x = 5;
echo "Original x: $x <br>"; // 5
$y = 5;
echo "Original y: $y <br>"; // 5
$a = true;
$b = false;
$first = "Hello";
$second = "World";
// Union
$union = $arr1 + $arr2;
echo "Union: ";
print_r($union); // Output: a => 1, b => 2
// Equality
echo "<br>arr1 == arr2: " . ($arr1 == $arr2 ? "true" :
"false") . "<br>"; // true
// Identity
echo "arr1 === arr2: " . ($arr1 === $arr2 ? "true" :
"false") . "<br>"; // false
// Inequality
echo "arr1 != arr3: " . ($arr1 != $arr3 ? "true" :
"false") . "<br>"; // false
// Non-identity
echo "arr1 !== arr3: " . ($arr1 !== $arr3 ? "true" :
"false") . "<br>"; // true
?>
9) Write a PHP program to demonstrate the use of Decision making control
structures using-If statement
<?php
// Using If statement
$number = 10;
if ($number > 0) {
echo "The number is positive.";
}
?>
10) Write a PHP program to demonstrate the use of Decision making control
structures using-If else statement
<?php
// Using If-Else statement
$number = -5;
if ($number > 0) {
echo "The number is positive.";
} else {
echo "The number is negative.";
}
?>
11) Write a PHP program to demonstrate the use of Decision making control
structures using-else if statement
<?php
// Using Else-If statement
$number = 0;
if ($number > 0) {
echo "The number is positive.";
} elseif ($number < 0) {
echo "The number is negative.";
} else {
echo "The number is zero.";
}
?>
12) Write a PHP program to demonstrate the use of Decision making control
structures using-switch statement
<?php
// Using Switch statement
$day = 3;
switch ($day) {
case 1:
echo "Monday";
break;
case 2:
echo "Tuesday";
break;
case 3:
echo "Wednesday";
break;
case 4:
echo "Thursday";
break;
case 5:
echo "Friday";
break;
case 6:
echo "Saturday";
break;
case 7:
echo "Sunday";
break;
default:
echo "Invalid day";
}
?>
13) Write a PHP program to demonstrate the use of Looping structures using-
while statement
<?php
// Using While loop
$count = 1;
$count = 1;
do {
echo "Count: $count <br>";
$count++;
} while ($count <= 5);
?>
15) Write a PHP program to demonstrate the use of Looping structures using-
for statement
<?php
// Using For loop
// Accessing elements
echo "First fruit: " . $fruits[0] . "<br>"; // Apple
echo "Second fruit: " . $fruits[1] . "<br>"; // Banana
// Removing an element
unset($fruits[1]); // Removes Banana
echo "After removal: ";
print_r($fruits); // Array ( [0] => Apple [2] => Cherry
[3] => Orange )
?>
18) Write a PHP program for creating and manipulating-Associative array
<?php
// Creating an associative array
$person = array("name" => "John", "age" => 25, "city"
=> "New York");
// Accessing elements
echo "Name: " . $person["name"] . "<br>"; // John
echo "Age: " . $person["age"] . "<br>"; // 25
// Subclass
class Dog extends Animal {
public function speak() {
echo "$this->name barks<br>";
}
}
process_form.php
<?php
// process_form.php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Collect form data
$name = $_POST['name'];
$gender = $_POST['gender'];
$hobbies = isset($_POST['hobbies']) ?
$_POST['hobbies'] : array();
process_form.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Collect form data
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$gender = $_POST['gender'];
$hobbies = isset($_POST['hobbies']) ?
$_POST['hobbies'] : [];
if (isset($_COOKIE[$name])) {
echo "Cookie '$name' is already set!<br>";
} else {
echo "Cookie '$name' is not set yet.<br>";
}
?>
read_cookie.php
<?php
// Check if the cookie is set
if (isset($_COOKIE["user"])) {
echo "Hello, " . $_COOKIE["user"] . "!<br>";
} else {
echo "Cookie 'user' is not set.<br>";
}
?>
USE userDB;
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-
width, initial-scale=1.0">
<title>Enter Data into Database</title>
</head>
<body>
<h2>Enter Your Information</h2>
<form method="POST" action="insert_data.php">
Name: <input type="text" name="name"
required><br><br>
Email: <input type="email" name="email"
required><br><br>
Phone: <input type="text" name="phone"
required><br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
insert_data.php
<?php
// Database connection details
$servername = "localhost"; // MySQL server (use
'localhost' if it's on the same machine)
$username = "root"; // Database username (default
is 'root' for local servers)
$password = ""; // Database password (default is
empty for local servers)
$dbname = "userDB"; // Database name
// Create connection
$conn = new mysqli($servername, $username,
$password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn-
>connect_error);
}
view_data.php
<?php
// Database connection details
$servername = "localhost"; // MySQL server (use
'localhost' for local server)
$username = "root"; // Database username (default is
'root' for local servers)
$password = ""; // Database password (default is
empty for local servers)
$dbname = "userDB"; // Database name
// Create connection
$conn = new mysqli($servername, $username,
$password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn-
>connect_error);
}
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>" . $row["id"] . "</td>
<td>" . $row["name"] . "</td>
<td>" . $row["email"] . "</td>
<td>" . $row["phone"] . "</td>
</tr>";
}
echo "</table>";
} else {
echo "No data found!";
}