Adv PHP Programs
1. Create a Form to Accept Customer's Details and Display it on
the Next Page
php
CopyEdit
<!-- customer_form.php -->
<form method="POST" action="display_customer.php">
Name: <input type="text" name="name" required><br>
Email: <input type="email" name="email" required><br>
Phone: <input type="text" name="phone" required><br>
<input type="submit" value="Submit">
</form>
<!-- display_customer.php -->
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
echo "<h3>Customer Details</h3>";
echo "Name: $name<br>";
echo "Email: $email<br>";
echo "Phone: $phone<br>";
}
?>
2. Write a PHP Script to Design a Form to Accept a Number from
the User to Check Whether the Number is Palindrome or Not
(Self-Processing Page)
Adv PHP Programs 1
php
CopyEdit
<!-- palindrome_form.php -->
<form method="POST" action="">
Enter a number: <input type="text" name="number" required>
<input type="submit" value="Check Palindrome">
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$num = $_POST['number'];
$reversed = strrev($num);
if ($num == $reversed) {
echo "<h3>$num is a Palindrome.</h3>";
} else {
echo "<h3>$num is not a Palindrome.</h3>";
}
}
?>
3. Write XML Script to Print the Names of the Students Present in
"Student.xml" File
xml
CopyEdit
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student>
<name>John Doe</name>
</student>
<student>
<name>Jane Smith</name>
Adv PHP Programs 2
</student>
</students>
php
CopyEdit
<?php
$xml = simplexml_load_file('Student.xml');
foreach ($xml->student as $student) {
echo "Student Name: " . $student->name . "<br>";
}
?>
4. Define a Class Employee Having Private Member id, name,
salary, dept. Define Parameterized Constructor. Create Object
and Display Details for Employee Having Maximum Salary.
php
CopyEdit
<?php
class Employee {
private $id;
private $name;
private $salary;
private $dept;
public function __construct($id, $name, $salary, $dept) {
$this->id = $id;
$this->name = $name;
$this->salary = $salary;
$this->dept = $dept;
}
Adv PHP Programs 3
public function displayDetails() {
echo "ID: $this->id<br>";
echo "Name: $this->name<br>";
echo "Salary: $this->salary<br>";
echo "Department: $this->dept<br>";
}
}
$emp1 = new Employee(101, 'John', 5000, 'HR');
$emp2 = new Employee(102, 'Jane', 7000, 'Finance');
$emp3 = new Employee(103, 'Sam', 8000, 'Marketing');
$employees = [$emp1, $emp2, $emp3];
$max_salary_employee = $employees[0];
foreach ($employees as $emp) {
if ($emp->salary > $max_salary_employee->salary) {
$max_salary_employee = $emp;
}
}
$max_salary_employee->displayDetails();
?>
5. Create an XML File with Book Details Available in "xyz
Bookstore" from Categories: Technical, General Knowledge,
Fitness
xml
CopyEdit
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<technical>
<book>
Adv PHP Programs 4
<title>Learn PHP</title>
<author>John Doe</author>
<price>25.99</price>
</book>
</technical>
<general_knowledge>
<book>
<title>General Knowledge 101</title>
<author>Jane Smith</author>
<price>15.99</price>
</book>
</general_knowledge>
<fitness>
<book>
<title>Fitness Guide</title>
<author>Alex Brown</author>
<price>20.99</price>
</book>
</fitness>
</bookstore>
6. Write PHP Script to Create CD Catalog Using XML File
php
CopyEdit
<?php
$xml = new SimpleXMLElement('<catalog/>');
$cd = $xml->addChild('cd');
$cd->addChild('title', 'Abbey Road');
$cd->addChild('artist', 'The Beatles');
$cd->addChild('price', '9.99');
$cd->addChild('year', '1969');
$cd = $xml->addChild('cd');
Adv PHP Programs 5
$cd->addChild('title', 'The Dark Side of the Moon');
$cd->addChild('artist', 'Pink Floyd');
$cd->addChild('price', '12.99');
$cd->addChild('year', '1973');
Header('Content-type: text/xml');
echo $xml->asXML();
?>
7. Write a PHP Program to Create Student Registration Form and
Display Student Information (Use Sticky Form Concept)
php
CopyEdit
<!-- student_form.php -->
<form method="POST" action="">
Name: <input type="text" name="name" value="<?php echo isset($_POST
['name']) ? $_POST['name'] : ''; ?>" required><br>
Age: <input type="number" name="age" value="<?php echo isset($_POST
['age']) ? $_POST['age'] : ''; ?>" required><br>
Email: <input type="email" name="email" value="<?php echo isset($_POST
['email']) ? $_POST['email'] : ''; ?>" required><br>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$age = $_POST['age'];
$email = $_POST['email'];
echo "Student Details:<br>";
echo "Name: $name<br>";
echo "Age: $age<br>";
Adv PHP Programs 6
echo "Email: $email<br>";
}
?>
8. Write a PHP Script for the Following: Design a Form to Accept a
Number from the User to Find the Sum of the Digits of That
Number.
php
CopyEdit
<!-- sum_of_digits_form.php -->
<form method="POST" action="">
Enter a number: <input type="text" name="number" required>
<input type="submit" value="Find Sum">
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$num = $_POST['number'];
$sum = array_sum(str_split($num));
echo "Sum of digits of $num is: $sum";
}
?>
9. Write a PHP Script to Display Server Information in Table
Format (Use $_SERVER )
php
CopyEdit
<?php
echo "<table border='1'>
Adv PHP Programs 7
<tr><th>Key</th><th>Value</th></tr>";
foreach ($_SERVER as $key => $value) {
echo "<tr><td>$key</td><td>$value</td></tr>";
}
echo "</table>";
?>
10. Write Script to Solve Following Questions Using
"Student.XML" File
i) Create a DOM Document Object and Load This XML File
php
CopyEdit
<?php
$dom = new DOMDocument();
$dom->load('Student.xml'); // Load the XML file
echo $dom->saveXML(); // Display XML content
?>
ii) Get the Output of this Document to the Browser and Print the Names of
Students in "Student.XML" File
php
CopyEdit
<?php
$dom = new DOMDocument();
$dom->load('Student.xml');
$students = $dom->getElementsByTagName('student');
foreach ($students as $student) {
echo $student->getElementsByTagName('name')->item(0)->nodeValue . "<
br>";
Adv PHP Programs 8
}
?>
11. Write PHP Script to Create 'product.xml' Containing p_no,
p_name, color, weight and Display Product XML in Table Format
php
CopyEdit
<?php
$xml = new SimpleXMLElement('<products/>');
$product = $xml->addChild('product');
$product->addChild('p_no', '101');
$product->addChild('p_name', 'Laptop');
$product->addChild('color', 'Silver');
$product->addChild('weight', '2.5kg');
$product = $xml->addChild('product');
$product->addChild('p_no', '102');
$product->addChild('p_name', 'Smartphone');
$product->addChild('color', 'Black');
$product->addChild('weight', '0.3kg');
Header('Content-type: text/xml');
echo $xml->asXML();
?>
12. Write a PHP Script to Read 'book.xml' and Print Book Details
in Tabular Format Using SimpleXML
php
CopyEdit
<?php
Adv PHP Programs 9
$xml = simplexml_load_file('book.xml');
echo "<table border='1'>
<tr><th>Book Code</th><th>Book Name</th><th>Author</th><th>Year</th
><th>Price</th></tr>";
foreach ($xml->book as $book) {
echo "<tr>
<td>$book->bookcode</td>
<td>$book->bookname</td>
<td>$book->author</td>
<td>$book->year</td>
<td>$book->price</td>
</tr>";
}
echo "</table>";
?>
Adv PHP Programs 10