PRACTICAL QUESTIONS OF PHP
FOR B. SC IT SEM 6 OF WEB PROGRAMMING
UNIT NAME :- PHP OF WEB PROGRAMMING
SUBJECT NAME:- MJ–12 (Th):- WEB PROGRAMMING
FOR B. Sc. IT.
SEM 6 F.Y.U.G.P.
Copyright © by Dr. Ajay kumar pathak
B. Sc. IT SEMESTER 6
NOTES BASED ON NEP
SUBJECT NAME: MJ–12 (Th): WEB
PROGRAMMING
(To be selected by the students from)
NOTES OF MJ–12 (PRACTICAL): WEB
PROGRAMMING
UNIT NAME :- PHP
PRACTICAL PROGRAM
**** NOTES
*****
Semester Examination and Distribution of Marks
INTERNAL MARKS :- 40
(WITH PRACTICAL IN THE MJ 12 (WEB PROGRAMMING ))
End Semester Examination (ESE) : 60 Marks
PHP PROGRAM FOR PRACTICAL
Q1. (Write a program
in PHP to read and write file using form control. )
ANS:- PART 1: STEPS TO WRITE A PHP PROGRAM
(DETAILED EXPLANATION)
Step 1: Install a Local Server, for PHP needs a server to run.
You can install:- XAMPP (Recommended for beginners)
WAMP / MAMP
After installing:-
Start Apache Server
Step 2: Create Project Folder
Go to:- C:\xampp\htdocs\
Create a folder:- file_project
Step 3: Create PHP File Inside file_project,
create a file:- index.php
Step 4: Basic PHP Syntax,
PHP code always starts with:-
<?php
// PHP code here
?>
Step 5: Create HTML Form in PHP,
We use a form to:- Take input
from user, Send data to server
Example:- HTML FILE
<form
method="post">
<textarea
name="data"></textarea>
<input type="submit"
name="write" value="Write File">
<input type="submit"
name="read" value="Read File">
</form>
Step 6: Get Form Data
Use $_POST:-
$data = $_POST['data'];
Step 7: Write File in PHP:-
Use fopen() and fwrite():
PHP FILE
$file =
fopen("test.txt", "w");
fwrite($file, $data);
fclose($file);
Step 8: Read File in PHP:-
PHP FILE
$file =
fopen("test.txt", "r");
$content = fread($file,
filesize("test.txt"));
fclose($file);
Step 9: Run the Program:-
Open browser and type:- http://localhost/file_project/index.php
PART 2: FULL WORKING PHP
PROGRAM (Direct Run Code)
(Write a program in PHP to read and write file using form control. )
Copy this entire code into index.php
PHP
FILE
<!DOCTYPE
html>
<html>
<head>
<title>File Read Write</title>
</head>
<body>
<h2>PHP
File Read and Write</h2>
<form
method="post">
<textarea name="data"
rows="5" cols="40" placeholder="Enter text
here..."></textarea><br><br>
<input type="submit"
name="write" value="Write">
<input type="submit"
name="read" value="Read">
</form>
<br>
<?php
$filename
= "test.txt";
//
WRITE OPERATION
if
(isset($_POST['write']))
{
if (!empty($_POST['data']))
{
$file = fopen($filename,
"w");
if ($file)
{
fwrite($file, $_POST['data']);
fclose($file);
echo "<h3>Data written
successfully!</h3>";
}
else
{
echo "<h3>Error opening
file!</h3>";
}
}
else
{
echo "<h3>Please enter some
text!</h3>";
}
}
//
READ OPERATION
if
(isset($_POST['read']))
{
if (file_exists($filename))
{
$file = fopen($filename,
"r");
if ($file)
{
$size = filesize($filename);
if ($size > 0)
{
$content = fread($file, $size);
echo "<h3>File
Content:</h3>";
echo
"<pre>$content</pre>";
}
else
{
echo "<h3>File is
empty!</h3>";
}
fclose($file);
}
else
{
echo "<h3>Error opening
file!</h3>";
}
}
else
{
echo "<h3>File does not
exist!</h3>";
}
}
?>
</body>
</html>
PROGRAM THE END
Q2 WRITE A PROGRAM IN PHP
TO ADD, UPDATE AND DELETE USING STUDENT DATABASE.
PART 1: Full Steps to Write & Run PHP Program
Step 1: Install XAMPP:-
Download & install XAMPP
Open XAMPP Control Panel
Start:- Apache, MySQL
Step 3: Create Project Folder
Go to:- C:\xampp\htdocs\
Create folder:- student_crud
Step 4: Create PHP File:-
Inside folder create:- index.php
Step 5: Create Database.
Open browser:- http://localhost/phpmyadmin
Create database:- student_db
Create table:- students
Table Structure:-
|
FIELD |
TYPE |
EXTRA |
|
id |
int |
auto_increment (primary key) |
|
Name |
VARCHAR(20) |
|
|
email |
VARCHAR(20) |
|
|
course |
VARCHAR(50) |
|
PART 2: FULL WORKING CODE (DIRECT RUN)
File
Name:- index.php
<?php
$conn = mysqli_connect("localhost",
"root", "", "student_db");
// Check connection
if (!$conn) {
die("Connection Failed: " . mysqli_connect_error());
}
// ADD STUDENT
if (isset($_POST['add'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$course = $_POST['course'];
$sql = "INSERT INTO students (name, email, course) VALUES
('$name','$email','$course')";
mysqli_query($conn, $sql);
}
// DELETE STUDENT
if (isset($_GET['delete'])) {
$id
= $_GET['delete'];
$sql = "DELETE FROM students WHERE id=$id";
mysqli_query($conn, $sql);
}
// UPDATE STUDENT
if (isset($_POST['update'])) {
$id
= $_POST['id'];
$name = $_POST['name'];
$email = $_POST['email'];
$course = $_POST['course'];
$sql = "UPDATE students SET name='$name', email='$email',
course='$course' WHERE id=$id";
mysqli_query($conn,
$sql);
}
// FETCH DATA
$result = mysqli_query($conn, "SELECT *
FROM students");
?>
<!DOCTYPE html>
<html>
<head>
<title>Student CRUD</title>
</head>
<body>
<h2>Add Student</h2>
<form method="post">
Name: <input type="text" name="name"
required><br><br>
Email: <input type="email" name="email"
required><br><br>
Course: <input type="text" name="course"
required><br><br>
<input type="submit" name="add" value="Add
Student">
</form>
<hr>
<h2>Student List</h2>
<table border="1"
cellpadding="10">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Course</th>
<th>Action</th>
</tr>
<?php while ($row =
mysqli_fetch_assoc($result)) { ?>
<tr>
<form method="post">
<td><?php echo $row['id']; ?></td>
<td>
<input type="text" name="name"
value="<?php echo $row['name']; ?>">
</td>
<td>
<input type="text" name="email"
value="<?php echo $row['email']; ?>">
</td>
<td>
<input type="text" name="course" value="<?php
echo $row['course']; ?>">
</td>
<td>
<input type="hidden" name="id"
value="<?php echo $row['id']; ?>">
<input type="submit" name="update"
value="Update">
<a href="index.php?delete=<?php echo $row['id']; ?>"
onclick="return confirm('Delete?')">Delete</a>
</td>
</form>
</tr>
<?php } ?>
</table>
</body>
</html>
PART 3: How to Run:-
Open browser:- http://localhost/student_crud/index.php
PART 4: Explanation (Important Points):-
Database Connection:- mysqli_connect("localhost",
"root", "", "student_db");
PHP:- Add
Data:-- INSERT INTO students
PHP:- Delete Data:- DELETE FROM students WHERE id =.......
PHP:- Update Data :- UPDATE students SET .....
PHP:- Fetch
Data :-
SELECT * FROM students
THE END PROGRAM
Q3 WRITE A PROGRAM IN PHP FOR SETTING AND RETRIEVING A COOKIE
1ST What is a Cookie? :- A cookie is a small
piece of data stored in the user’s browser by a website.
Why Cookies are Used:- To remember user information, Save login
details, Store preferences (language,
theme, etc.)
Example:-
When you login to a website → it remembers you, That is done using cookies
PART 2: Syntax of Cookie in PHP,
PHP PROGRAM
Set Cookie:- setcookie(name, value, expiry_time, path);
PHP PROGRAM
Get Cookie:- $_COOKIE['name'];
PART 3: Steps to Create PHP Cookie Program
Step 1: Start XAMPP, Open XAMPP
Start:-
Apache
Step 2: Create Folder:-
Go to:-
C:\xampp\htdocs\
Create folder:- cookie_project
Step 3: Create File:-
File Name:-
index.php
Step 4: Write Code (FULL WORKING CODE)
Copy
this code exactly:-
PHP PROGRAM
<?php
// SET COOKIE
if (isset($_POST['set'])) {
$name = $_POST['username'];
//
set cookie for 1 hour
setcookie("user", $name, time() + 3600);
echo "<h3>Cookie is set!</h3>";
}
// DELETE COOKIE
if (isset($_POST['delete'])) {
setcookie("user", "", time() - 3600);
echo "<h3>Cookie Deleted!</h3>";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Cookie Program</title>
</head>
<body>
<h2>Set and Get Cookie in
PHP</h2>
<form method="post">
Enter Name: <input type="text" name="username"
required><br><br>
<input type="submit" name="set" value="Set
Cookie">
<input type="submit" name="get" value="Get
Cookie">
<input type="submit" name="delete"
value="Delete Cookie">
</form>
<br>
<?php
// GET COOKIE
if (isset($_POST['get']))
{
if
(isset($_COOKIE['user']))
{
echo "<h3>Welcome " . $_COOKIE['user'] .
"</h3>";
}
else
{
echo "<h3>No Cookie Found!</h3>";
}
}
?>
</body>
</html>
PART 5: How to Run:- Open browser:- http://localhost/cookie_project/index.php
THE PROGRAM END
Q4 WRITE
A PHP PROGRAM TO CREATE A SIMPLE WEBPAGE OF A COLLEGE.
PART 1:- This
program creates a simple college webpage using PHP
It displays:-
College Name,
Address,
Courses
PART 2: Steps to Write PHP Program
Step 1:- Install XAMPP,
Install XAMPP
Start:- Apache,
Step 2: Create Project Folder:-
Go to:- C:\xampp\htdocs\
Create folder:- college_site
Step 3: Create File:-
File Name:- index.php
Step 4: Write FULL WORKING CODE
Copy this exactly into index.php:-
PHP PROGRAM
<?php
// PHP VARIABLES
$college = "MRS. KMPM VOCATIONAL COLLEGE";
$address = "JAMSHEDPUR, Jharkhand";
$courses = ["BCA", "MCA",
"B.Tech", "MBA"];
?>
<!DOCTYPE html>
<html>
<head>
<title>College Webpage</title>
</head>
<body>
<h1><?php echo $college;
?></h1>
<p><b>Address:</b> <?php
echo $address; ?></p>
<h2>Courses Offered</h2>
<ul>
<?php
foreach($courses as $course) {
echo "<li>$course</li>";
}
?>
</ul>
</body>
</html>
PART 3: How to Run
Open browser:- http://localhost/college_site/index.php
PART 4: Output
You will see:
College Name
Address
Course List
THE PROGRAM END
Q5. WRITE
A PROGRAM IN PHP FOR EXCEPTION HANDLING FOR I) DIVIDE BY ZERO II) CHECKING DATE
FORMAT.
PART 1: What is Exception Handling in PHP?
Exception Handling is used to handle runtime
errors in a program.
PART 2: Steps to Write PHP Program
Step 1: Install XAMPP
Install XAMPP
Start:- Apache
Step 2: Create Project Folder
Go to:- C:\xampp\htdocs\
Create folder:- exception_project
Step 3: Create File
File Name:- index.php
PART 3: FULL WORKING CODE (DIRECT RUN)
Copy this exactly into index.php
PHP PROGRAM
<?php
// FUNCTION: DIVIDE
function divide($a, $b)
{
if
($b == 0) {
throw new Exception("Error: Cannot divide by zero!");
}
return $a / $b;
}
// FUNCTION: CHECK DATE FORMAT
function checkDateFormat($date)
{
$d
= DateTime::createFromFormat('Y-m-d', $date);
if
(!$d || $d->format('Y-m-d') !== $date)
{
throw new Exception("Error: Invalid date format! Use
YYYY-MM-DD");
}
return "Valid Date Format";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Exception Handling</title>
</head>
<body>
<h2>Exception Handling in
PHP</h2>
<form method="post">
Enter Number 1: <input type="number" name="num1"
required><br><br>
Enter Number 2: <input type="number" name="num2"
required><br><br>
Enter Date (YYYY-MM-DD): <input type="text"
name="date" required><br><br>
<input type="submit" name="check"
value="Check">
</form>
<br>
<?php
if (isset($_POST['check']))
{
// DIVISION CHECK
try
{
$result = divide($_POST['num1'], $_POST['num2']);
echo "<h3>Division Result: $result</h3>";
}
catch (Exception $e) {
echo "<h3>" . $e->getMessage() .
"</h3>";
}
// DATE CHECK
try
{
$msg = checkDateFormat($_POST['date']);
echo "<h3>$msg</h3>";
}
catch (Exception $e) {
echo "<h3>" . $e->getMessage() .
"</h3>";
}
}
?>
</body>
</html>
PART 4: How to Run
Open browser:- http://localhost/exception_project/index.php
PART 5: Explanation of Code
Throw:- throw
new Exception("Error message");
Used to generate error
Try:-
try {
//
risky code
}
Code that may cause error
PART 6: Output Behavior
|
INPUT |
OUTPUT |
|
10/2 |
5 |
|
10/0 |
ERROR |
|
2026-12-01 |
VALID |
|
2025-13-01 |
INVALID |
THE END PROGRAM
No comments:
Post a Comment
PLEASE DO LEAVE YOUR COMMENTS