1. Write a program using Conditional Structure.
Program: -
<!DOCTYPE html>
<html>
<body>
<?php
$t = date("H");
echo "<p>The hour (of the server) is " . $t;
echo ", and will give the following message:</p>";
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
?>
</body>
</html>
Output: -
2. Write a program using array which accepts five values and print the values.
Program: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<?php
$a = array(1,2,3,4,5);
for ($i=0; $i < 5; $i++) {
echo $a[$i]."<br>";
?>
</body>
</html>
Output: -
3. Write a program using Looping Structure with concept of Functions.
Program: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<?php
function table($n)
for ($i=1; $i <= 10; $i++) {
echo "$n * $i = ".$n*$i."<br>";
table(5);
?>
</body>
</html>
Output: -
4. Write a program in PHP for type casting of a variable
Program: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>Practical-1</title>
</head>
<body>
<?php
$a = 12.5;
echo "the float value : $a<br>";
$k=(int)$a;
echo "converting into int value : $k<br>";
$k=(string)$a;
echo "converting into string : $k<br>";
?>
</body>
</html>
Output: -
5. Write a program in PHP to display Multiplication Table using nested for loop.
Program: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>Practical-2</title>
</head>
<body>
<?php
$x = 5;
$y = 10;
echo "-----------multiplication table from $x to $y -----------<br>";
for ($x; $x <= $y; $x++) {
for($j = 1; $j <= 10; $j++)
echo "$x * $j = ".$x*$j." ";
echo "<br>";
?>
</body>
</html>
Output: -
6. Write a program In PHP to Sort an array using Bubble Sort function.
Program: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>Practical-3</title>
</head>
<body>
<?php
function bubble_Sort($my_array)
do
$swapped = false;
for( $i = 0, $c = count( $my_array ) - 1; $i < $c; $i++ )
if( $my_array[$i] > $my_array[$i + 1] )
list( $my_array[$i + 1], $my_array[$i] ) =
array( $my_array[$i], $my_array[$i + 1] );
$swapped = true;
while( $swapped );
return $my_array;
$test_array = array(7,5,3,1,9,-8,-5, -1);
echo "Original Array :<br>";
echo implode(', ',$test_array );
echo "<br>Sorted Array:<br>";
echo implode(', ',bubble_Sort($test_array));
?>
</body>
</html>
Output: -
7. Design the personal information form ,submit and retrieve the form data using php
$_POST,$_GET,$ REQUEST variable.
Program: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>Practical-4</title>
</head>
<body>
<form method="POST">
Full Name : <input type="text" name="full_name" placeholder="Full Name"><br><br>
Enter Email : <input type="email" name="EmailID" placeholder="Enter Email"><br><br>
Enter Address : <textarea name="address" cols="40" rows="5"
placeholder="Address"></textarea><br><br>
Mobile No : <input type="number" name="mobile_no" placeholder="Mobile
Number"><br><br>
<input type="submit" value="Submit" name="submit"><br><br>
</form>
<?php
if (isset($_POST['submit']))
echo $_POST['full_name']."<br>";
echo $_POST['EmailID']."<br>";
echo $_POST['address']."<br>";
echo $_POST['mobile_no']."<br>";
}
?>
</body>
</html>
Output: -
8. Study of Cookies and Session In PHP.
a. Use of Cookies
Program: -
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Practical-5(Cookies)</title>
</head>
<body>
<?php
setcookie("category", "Books", time()+ 3600, "/");
if(isset($_COOKIE["category"]))
echo "Your Cookie is = ".$_COOKIE["category"];
else
echo "Please Set Your Cookie";
?>
</body>
</html>
Output: -
Use Of Session
i) P5_Session_Create
Program: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>Session Create</title>
</head>
<body>
<?php
session_start();
$_SESSION['username'] = "PHP";
$_SESSION['FavCat'] = "Books";
echo "Session is Saved";
?>
</body>
</html>
Output: -
ii) P5_Session_Access
Program: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>Session Accessed</title>
</head>
<body>
<?php
session_start();
if(isset($_SESSION['username']))
echo "Wellcome ".$_SESSION['username']."<br>";
echo "Your Favorite category is ".$_SESSION['FavCat']."<br>";
}
else
echo "Please loge in first";
?>
</body>
</html>
Output: -
iii) P5_Session_Close
Program: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>Session Closed</title>
</head>
<body>
<?php
session_start();
session_unset();
session_destroy();
echo "You are loge out";
?>
</body>
</html>
Output: -
9. Study Of Mysql DDL,DML,DCL Commands.
DDL(Data Definition Language): - DDL or Data Definition Language actually consists of the
SQL commands that can be used to define the database schema. It simply deals with descriptions
of the database schema and is used to create and modify the structure of database objects in the
database.
Examples of DDL commands:
CREATE – is used to create the database or its objects (like table, index, function, views, store
procedure and triggers).
DROP – is used to delete objects from the database.
ALTER-is used to alter the structure of the database.
TRUNCATE–is used to remove all records from a table, including all spaces allocated for the
records are removed.
COMMENT –is used to add comments to the data dictionary.
RENAME –is used to rename an object existing in the database.
DML(Data Manipulation Language): - The SQL commands that deals with the manipulation
of data present in the database belong to DML or Data Manipulation Language and this includes
most of the SQL statements.
Examples of DML:
INSERT – is used to insert data into a table.
UPDATE – is used to update existing data within a table.
DELETE – is used to delete records from a database table.
DCL(Data Control Language): - DCL includes commands such as GRANT and REVOKE
which mainly deal with the rights, permissions and other controls of the database system.
Examples of DCL commands:
GRANT-gives user’s access privileges to the database.
REVOKE-withdraw user’s access privileges given by using the GRANT command.
10. Design Login Form and Validate that form using PHP Code.
(Login Page)
Program: -
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Form</title>
<style>
#LoginCon{
border: 1px solid black;
margin: 9px 595px;
padding: 5px;
#formID{
margin: 5px 20px;
padding: 10px 16px;
#btn{
padding: 8px 97px;
margin: 11px -2px;
#pass{
margin: 1px 7px;
</style>
</head>
<body>
<div id = "LoginCon">
<form action="P7_ValidateLoginForm.php" method="POST" required id = "formID">
<label for="username">UserName</label>
<input type="text" name = "username" placeholder = "Enter UserName"
required><br><br>
<label for="password">Password</label>
<input type="password" name = "password" placeholder = "Enter Password" required id =
"pass"><br>
<input type="submit" value="Submit" name = "submit" id = "btn">
</form>
</div>
</body>
</html>
(Login Validation)
Program: -
<?php
if (isset($_POST['submit']))
{
$servername = "localhost";
$username = "root";
$password = "123456";
$database = "Practical";
$Conn = mysqli_connect($servername, $username, $password, $database);
$SQL_query = "SELECT * FROM `login`";
$result = mysqli_query($Conn, $SQL_query);
$numRows = mysqli_num_rows($result);
$ststus = 0;
if($numRows > 0)
$i = 1;
while($i <= $numRows)
$row = mysqli_fetch_assoc($result);
if(($row['user_name'] == $_POST['username']) && ($row['password'] ==
$_POST['password']))
$ststus = 1;
break;
$i++;
if($ststus == 1)
echo "done";
else
echo "try again";
?>
Output: -
11. Write a PHP code to create database and table in Mysql.
(TO Create DataBase)
Program: -
<?php
$servername = "localhost";
$username = "root";
$password = "123456";
$Conn = mysqli_connect($servername, $username, $password); //To form connection with SQL
$SQL_query = "CREATE DATABASE Practical"; //SQL Query to create database
$result = mysqli_query($Conn, $SQL_query); //TO execute query
if($result)
echo "Database Save";
else
echo "Database not Save Because : ".mysqli_error($Conn);
?>
(TO Create Table)
Program: -
<?php
$servername = "localhost";
$username = "root";
$password = "123456";
$database = "Practical"
$Conn = mysqli_connect($servername, $username, $password, $database); //To form
connection with SQL
//To Create Table
$SQL_query2 = "CREATE TABLE `login` ( `user_id` INT NOT NULL , `user_name`
VARCHAR(500) NOT NULL , `password` VARCHAR(500) NOT NULL , `email`
VARCHAR(100) NOT NULL );";
$result = mysqli_query($Conn, $SQL_query2); //TO execute query
if($result)
echo "Table Save";
else
echo "Table not Save Because : ".mysqli_error($Conn);
?>
12. Write a PHP code to insert, delete, select the data from database.
(Insert ata into DataBase)
Program: -
<?php
$servername = "localhost";
$username = "root";
$password = "123456";
$database = "Practical";
$Conn = mysqli_connect($servername, $username, $password, $database); //To form
connection with SQL
$SQL_query = "INSERT INTO `login` (`user_id`, `user_name`, `password`, `email`) VALUES
('1', 'Rajat@rai', 'r@458$$', '
[email protected]')";
$result = mysqli_query($Conn, $SQL_query); //TO execute query
if($result)
echo "Data Save";
else
echo "Data not Save Because : ".mysqli_error($Conn);
?>
(Delete data from database)
Program: -
<?php
$servername = "localhost";
$username = "root";
$password = "123456";
$database = "Practical";
$Conn = mysqli_connect($servername, $username, $password, $database); //To form
connection with SQL
$SQL_query = "delete from login where user_id = 1";
$result = mysqli_query($Conn, $SQL_query); //TO execute query
if($result)
echo "Data Deleted";
else
echo "Data not Deleted : ".mysqli_error($Conn);
?>
(SELECT Data From Table)
Program: -
<?php
$servername = "localhost";
$username = "root";
$password = "123456";
$database = "Practical";
$Conn = mysqli_connect($servername, $username, $password, $database); //To form
connection with SQL
$SQL_query = "SELECT * FROM `login`"; //SQL Query
$result = mysqli_query($Conn, $SQL_query); //TO execute query
$numRows = mysqli_num_rows($result); //return the number of rows
if($numRows > 0) //to check whether we have data or not in table
$i = 1;
while($i <= $numRows)
$row = mysqli_fetch_assoc($result); //return the 1 row in array form
echo $row['user_id']." | ".$row['user_name']." | ".$row['password']." |
".$row['email']."<br>"; //to print only value
$i++;
?>
Output: -
13. Design a from which upload & Display image in PHP
<html>
<head>
<title>PHP File Upload example</title>
</head>
<body>
<form action="P10_UploadImage.php" enctype="multipart/form-data" method="post">
Select image :
<input type="file" name="file"><br/>
<input type="submit" value="Upload" name="Submit1"> <br/>
</form>
<?php
if(isset($_POST['Submit1']))
$filepath = "images/" . $_FILES["file"]["name"];
if(move_uploaded_file($_FILES["file"]["tmp_name"], $filepath))
echo "<img src=".$filepath." height=200 width=300>";
else
{
echo "Error !!";
?>
</body>
</html>
Output: -
14. Write a PHP Code to make database connection, Create Data Base, Create Table In
Mysql.
Program: -
<?php
function Create_DB()
$servername = "localhost";
$username = "root";
$password = "123456";
$Conn = mysqli_connect($servername, $username, $password); //To form connection with SQL
$SQL_query = "CREATE DATABASE P14"; //SQL Query to create database
$result = mysqli_query($Conn, $SQL_query); //TO execute query
if($result)
echo "Database Save";
else
echo "Database not Save Because : ".mysqli_error($Conn);
}
function Create_Table()
$servername = "localhost";
$username = "root";
$password = "123456";
$database = "P14";
$Conn = mysqli_connect($servername, $username, $password, $database); //To form
connection with SQL
$SQL_query2 = "CREATE TABLE `login` ( `user_id` INT NOT NULL , `user_name`
VARCHAR(500) NOT NULL , `password` VARCHAR(500) NOT NULL , `email`
VARCHAR(100) NOT NULL );";
$result = mysqli_query($Conn, $SQL_query2); //TO execute query
if($result)
echo "Table Save";
else
echo "Table not Save Because : ".mysqli_error($Conn);
Create_DB();
Create_Table();
?>
Output: -
15. Write a PHP code Insert, Delete, Update, Select the Data From Data Base.
Program: -
<?php
function Insert()
$servername = "localhost";
$username = "root";
$password = "123456";
$database = "P14";
$Conn = mysqli_connect($servername, $username, $password, $database); //To form
connection with SQL
$SQL_query = "INSERT INTO `login` (`user_id`, `user_name`, `password`, `email`)
VALUES ('1', 'Rajat@rai', 'r@458$$', '
[email protected]'), ('2', 'Rahul@rai', 'r@jh8$$',
'
[email protected]')";
$result = mysqli_query($Conn, $SQL_query); //TO execute query
if($result)
echo "Data Save";
else
echo "Data not Save Because : ".mysqli_error($Conn);
function Delete()
{
$servername = "localhost";
$username = "root";
$password = "123456";
$database = "P14";
$Conn = mysqli_connect($servername, $username, $password, $database); //To form
connection with SQL
$SQL_query = "delete from login where user_id = 1";
$result = mysqli_query($Conn, $SQL_query); //TO execute query
if($result)
echo "<br>Data Deleted";
else
echo "<br>Data not Deleted : ".mysqli_error($Conn);
function Update()
$servername = "localhost";
$username = "root";
$password = "123456";
$database = "P14";
$Conn = mysqli_connect($servername, $username, $password, $database); //To form
connection with SQL
$SQL_query = "Update login set user_name = 'Shubham', password = '@wedf#$', email =
'
[email protected]' where user_id = 2";
$result = mysqli_query($Conn, $SQL_query); //TO execute query
if($result)
echo "<br>Data Update";
else
echo "<br>Data not Update : ".mysqli_error($Conn);
function Select()
$servername = "localhost";
$username = "root";
$password = "123456";
$database = "P14";
$Conn = mysqli_connect($servername, $username, $password, $database); //To form
connection with SQL
$SQL_query = "SELECT * FROM `login`"; //SQL Query
$result = mysqli_query($Conn, $SQL_query); //TO execute query
$numRows = mysqli_num_rows($result); //return the number of rows
if($numRows > 0) //to check whether we have data or not in table
$i = 1;
while($i <= $numRows)
$row = mysqli_fetch_assoc($result); //return the 1 row in array form
// echo var_dump($row)."<br>"; //to print the array row form one by one
echo $row['user_id']." | ".$row['user_name']." | ".$row['password']." |
".$row['email']."<br>"; //to print only value
$i++;
Insert();
delete();
Update();
Select();
?>
Output: -