PHP Programs for Beginners
1. Array Functions
PHP Code:
<?php
// Simple array functions for beginners
// 1. Create an array
$numbers = array(1, 2, 3, 4, 5);
// 2. Sum of array elements
echo "Sum: " . array_sum($numbers) . "<br>";
// 3. Maximum value in array
echo "Maximum: " . max($numbers) . "<br>";
// 4. Sorting array in ascending order
sort($numbers);
echo "Sorted Asc: " . implode(", ", $numbers) . "<br>";
// 5. Reversing the array
$reversed = array_reverse($numbers);
echo "Reversed: " . implode(", ", $reversed) . "<br>";
?>
Output:
Sum: 15
Maximum: 5
Sorted Asc: 1, 2, 3, 4, 5
Reversed: 5, 4, 3, 2, 1
2. User Defined Functions
PHP Code:
<?php
// User defined function to add two numbers
function add($a, $b) {
return $a + $b;
}
// Using the function
echo "Sum of 10 and 20: " . add(10, 20) . "<br>";
// Function to find square of a number
function square($n) {
return $n * $n;
}
echo "Square of 5: " . square(5) . "<br>";
?>
Output:
Sum of 10 and 20: 30
Square of 5: 25
3. Factorial of a Number
PHP Code:
<?php
// Function to calculate factorial
function factorial($n) {
if ($n == 0) return 1;
else return $n * factorial($n - 1);
}
// Calculate factorial of 5
echo "Factorial of 5: " . factorial(5) . "<br>";
?>
Output:
Factorial of 5: 120
4. File Manipulation
PHP Code:
<?php
// Creating a simple text file and writing data to it
$file = fopen("testfile.txt", "w");
fwrite($file, "Hello, this is a test file.");
fclose($file);
// Reading the file and displaying the contents
$file = fopen("testfile.txt", "r");
echo "File Contents: " . fread($file, filesize("testfile.txt"));
fclose($file);
?>
Output:
File Contents: Hello, this is a test file.
5. Session
PHP Code:
<?php
// Starting a session
session_start();
// Storing a session variable
$_SESSION["username"] = "JohnDoe";
// Displaying session variable
echo "Session Username: " . $_SESSION["username"];
?>
Output:
Session Username: JohnDoe
6. Cookie Creation
PHP Code:
<?php
// Setting a cookie
setcookie("username", "JohnDoe", time() + (86400 * 30), "/"); // 1 day expiry
// Displaying the cookie if it's set
if (isset($_COOKIE["username"])) {
echo "Cookie Username: " . $_COOKIE["username"];
} else {
echo "Cookie not set!";
}
?>
Output:
Cookie Username: JohnDoe
7. Travel Agency Website (Basic Structure)
PHP Code:
<!DOCTYPE html>
<html>
<head>
<title>Travel Agency</title>
</head>
<body>
<h1>Welcome to XYZ Travel Agency</h1>
<p>Book your dream vacation with us!</p>
<ul>
<li>Destination: Paris</li>
<li>Duration: 7 Days</li>
<li>Cost: $1500</li>
</ul>
</body>
</html>
Output:
Displays a basic webpage with travel package details.
8. Table Creation using MySQL
PHP Code:
<?php
// Assuming connection is already established
$sql = "CREATE TABLE Customers (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50)
)";
if ($conn->query($sql) === TRUE) {
echo "Table Customers created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
?>
Output:
Table Customers created successfully
9. Insertion in Table using MySQL
PHP Code:
<?php
// Assuming connection is already established
$sql = "INSERT INTO Customers (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
Output:
New record created successfully
10. Update Table using MySQL
PHP Code:
<?php
// Assuming connection is already established
$sql = "UPDATE Customers SET lastname='Smith' WHERE id=1";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
?>
Output:
Record updated successfully
11. Display Table using MySQL
PHP Code:
<?php
// Assuming connection is already established
$sql = "SELECT id, firstname, lastname, email FROM Customers";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " .
$row["lastname"]. " - Email: " . $row["email"]. "<br>";
}
} else {
echo "0 results";
}
?>
Output:
Displays records from the Customers table.