160450320023
Practical 1
Aim : Creating the PHP page.
Source Code :
<?php
echo "Hello World";
?>
Output :
Page | 1
160450320023
Practical 2
Aim : Programs using arrays and control and loop structures.
Source Code :
<?php
echo "<br>control and loop structure<br>";
$count = 0;
$num = 2;
while ($count<15 )
{
$div_count=0;
for ( $i=1; $i<=$num; $i++)
{
if (($num%$i)==0)
{
$div_count++;
}
}
if ($div_count<3)
{
echo $num.",";
$count=$count+1;
}
$num=$num+1;
}
echo "<br>Numbered array<br>";
$season=array("summer","winter","spring","autumn");
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
Page | 2
160450320023
Echo "<br>Association array<br>";
$salary= array("shinu"=>"350000","john"=>"4500000","kartik"=>"200000");
echo "Shinu salary : ".$salary["shinu"]."<br/>";
echo "John salary : ".$salary["john"]."<br/>";
echo "Kartik salary : ".$salary["kartik"]."<br/>";
?>
Output :
Page | 3
160450320023
Practical 3
Aim : Testing different PHP functions and user define function.
Source Code :
<?php
echo "<br>Array functions<br>";
$a = array(60,80,15,17,25,"key"=>null);
$a1 = array(65,85,35,47,95);
array_push($a,50,20,40,10,90,30,70);
echo "<br>Adding<br>";
print_r($a);
echo "<br>Removing<br>";
unset($a[2]);
print_r($a);
echo "<br>Searching<br>";
if(array_key_exists(0,$a))
{
echo "key is exists<br/>";
}
else
{
echo "key is not exists<br/>";
}
echo "<br>Counting<br>";
echo count($a);
echo "<br>Margeing<br>";
$a2 = array_merge($a,$a1);
print_r($a2);
echo "<br>Unset<br>";
Page | 4
160450320023
unset($a[5]);
print_r($a);
echo "<br>Sorting<br>";
sort($a);
$count = count($a);
for($i=0;$i<$count;$i++)
{
echo "$a[$i]<br>";
}
echo "<br>Isset<br>";
if(isset($a['key']))
{
echo "true";
}
else
{
echo "flase";
}
?>
Page | 5
160450320023
Output :
Page | 6
160450320023
<?php
echo "<br>Using of string funtion<br>";
$str = " HeLlo";
$str1 = "b";
$str2 = "A";
$a = 5;
$b = 10;
echo "<br>Using of chr funtion<br>";
echo chr(76);
echo "<br>Using of ord funtion<br>";
echo ord('h');
echo "<br>Using of strtolower funtion<br>";
echo strtolower($str);
echo "<br>Using of strtoupper funtion<br>";
echo strtoupper($str);
echo "<br>Using of strlen funtion<br>";
echo strlen($str);
echo "<br>Using of ltrim funtion<br>";
echo ltrim($str);
echo "<br>Using of rtrim funtion<br>";
echo strlen(rtrim($str));
echo "<br>Using of trim funtion<br>";
echo strlen(trim($str));
echo "<br>Using of strcmp funtion<br>";
echo strcmp($str1,$str2);
echo "<br>Using of strcasecmp funtion<br>";
echo strcasecmp($str1,$str2);
echo "<br>Using of strstr funtion<br>";
Page | 7
160450320023
echo strstr("hello world","w");
?>
Output :
Page | 8
160450320023
<?php
$a = 5;
$b = 10;
echo "<br>Using of math funtion<br>";
echo "<br>Using of ceil funtion<br>";
echo ceil(1.4);
echo "<br>Using of floor funtion<br>";
echo floor(2.4);
echo "<br>Using of round funtion<br>";
echo round(5.4);
echo "<br>Using of abs funtion<br>";
echo abs(-7);
echo "<br>Using of min funtion<br>";
echo "min value is<br/>".min($a,$b);
echo "<br>Using of max funtion<br>";
echo "max value is<br/>".max($a,$b);
echo "<br>Using of pow funtion<br>";
echo "power value is<br/>".pow(5,2);
echo "<br>Using of sqrt funtion<br>";
echo "squrt root value is<br/>".sqrt(5);
echo "<br>Using of fmod funtion<br>";
echo fmod($a,$b);
?>
Output:
Page | 9
160450320023
Page | 10
160450320023
<?php
echo "<br>Using of date and time funtion<br>";
echo "<br>Using of data funtion<br>";
echo "Today's date is<br/>".date("y-m-d h:m:s");
echo "<br>Using of get_date funtion<br>";
print_r(getdate());
echo "<br>Using of checkdate funtion<br>";
if(checkdate(32,10,2021))
{
echo "valid";
}
else
{
echo "invalid";
}
echo "<br>Using of mktime funtion<br>";
$d = mktime(11,14,54,7,12,2021);
echo "Create date is<br/>".date("y-m-d",$d);
echo "<br>Using of time funtion<br>";
//$t = time();
echo time()."<br>";
echo date("y-m-d");
?>
Page | 11
160450320023
Output :
Page | 12
160450320023
<?php
echo "User define function<br/>";
function cube($x)
{
return $x * $x * $x;
}
echo "The cube of 4 is : ".cube(4)."<br />";
echo "The cube of 9 is : ".cube(9)."<br />";
echo "The cube of 20 is : ".cube(20)."<br />";
?>
Output :
Page | 13
160450320023
Practical 4
Aim : Testing different PHP functions and user define function.
Source Code :
GET
<html>
<head>
<title>first php page</title>
</head>
<body>
<form action ="form2.php" method=GET>
first name :
<input type="text" name="name" placeholder="enter your frist name">
<br>
<br>
last name :
<input type="text" name="lastname" placeholder="enter your last name">
<br>
<br>
email :
<input type="text" name="email" placeholder="enter your email">
<br>
<br>
password :
<input type="password" name="password" placeholder="enter your password">
<br>
<br>
number :
<input type="text" name="number" placeholder="enter your number">
Page | 14
160450320023
<br>
<br>
<select>
<option value="HTML">Select a Language</option><br>
<option value="Hindi">Hindi</option>
<option value="English">English</option>
<option value="Gujarati">Gujarati</option>
</select>
<br>
<br>
<input type="submit" name ="submit" value="submit">
</from>
</body>
</html>
<?php
if(isset($_GET['name']))
{
$name = $_GET['name'];
}
else
{
$name = " John - This is our default name";
}
echo 'first name: '.$name;
if(isset($_GET['lastname']))
{
$n = $_GET['lastname'];
}
Page | 15
160450320023
else
{
$n = " Joy - This is our default name";
}
echo 'last name: '.$n;
?>
Output :
Page | 16
160450320023
Page | 17
160450320023
POST
Source Code :
<html>
<head>
<title>first php page</title>
</head>
<body>
<form action ="form2.php" method=POST>
first name :
<input type="text" name="name" placeholder="enter your frist name">
<br>
<br>
last name :
<input type="text" name="lastname" placeholder="enter your last name">
<br>
<br>
email :
<input type="text" name="email" placeholder="enter your email">
<br>
<br>
password :
<input type="password" name="password" placeholder="enter your password">
<br>
<br>
number :
<input type="text" name="number" placeholder="enter your number">
<br>
<br>
Page | 18
160450320023
<select>
<option value="HTML">Select a Language</option><br>
<option value="Hindi">Hindi</option>
<option value="English">English</option>
<option value="Gujarati">Gujarati</option>
</select>
<br>
<br>
<input type="submit" name ="submit" value="submit">
</from>
</body>
</html>
<?php
if(isset($_POST['name']))
{
$name = $_POST['name'];
}
else
{
$name = " John - This is our default name";
}
echo 'first name: '.$name;
if(isset($_POST['lastname']))
{
$n = $_POST['lastname'];
}
else
{
Page | 19
160450320023
$n = " Joy - This is our default name";
}
echo 'last name: '.$n;
?>
Output:
Page | 20
160450320023
Page | 21
160450320023
Practical-5
Aim: Passing hidden information to the form processing script via hidden form controls and a URL
query string.
Source code:
<html>
<head>
<title>first php page</title>
</head>
<body>
<form action ="form2.php" method=GETT>
first name :
<input type="text" name="name" placeholder="enter your frist name">
<br>
<br>
last name :
<input type="text" name="lastname" placeholder="enter your last name">
<br>
<br>
email :
<input type="text" name="email" placeholder="enter your email">
<br>
<br>
password :
<input type="password" name="password" placeholder="enter your password">
<br>
<br>
number :
<input type="hidden" name="number" value="9987654702">
<br>
<br>
Page | 22
160450320023
<select>
<option value="HTML">Select a Language</option><br>
<option value="Hindi">Hindi</option>
<option value="English">English</option>
<option value="Gujarati">Gujarati</option>
</select>
<br>
<br>
<input type="submit" name ="submit" value="submit">
</from>
</body>
</html>
<?php
if(isset($_GET['name']))
{
$name = $_GET['name'];
}
else
{
$name = " John - This is our default name";
}
echo 'first name: '.$name;
if(isset($_GET['lastname']))
{
$n = $_GET['lastname'];
}
else
{
$n = " Joy - This is our default name";
}
Page | 23
160450320023
echo 'last name: '.$n;
?>
Output:
Page | 24
160450320023
Practical-6
Aim: Creating forms with sessions and cookies.
Source code:
COOKIES:
<?php
?>
<form action="page2.php" method="post" style="border: 2px dotted blue; text-align:center; width: 400px;">
<p>
Username: <input name="username" type="text" value="<?php
if(isset($_COOKIE["username"])) { echo $_COOKIE["username"]; } ?>" class="input-field">
</p>
<p>Password: <input name="password" type="password" value="<?php
if(isset($_COOKIE["password"])) { echo $_COOKIE["password"]; } ?>" class="input-field">
</p>
<p><input type="checkbox" name="remember" /> Remember me
</p>
<p><input type="submit" value="Login"></span></p>
</form>
<?php
if(!empty($_POST["remember"])) {
setcookie ("username",$_POST["username"],time()+ 3600);
setcookie ("password",$_POST["password"],time()+ 3600);
echo "Cookies Set Successfuly";
} else {
setcookie("username","");
setcookie("password","");
echo "Cookies Not Set";
}
?>
Page | 25
160450320023
<p><a href="page1.php"> Go to Login Page </a> </p>
Output:
SESSION:
<?php
Page | 26
160450320023
session_start();
?>
<html>
<body>
<?php
$_SESSION["user"] = "Sachin";
echo "Session information are set successfully.<br/>";
?>
<a href="ses2.php">Visit next page</a>
</body>
</html>
<?php
session_start();
?>
<html>
<body>
<?php
echo "User is: ".$_SESSION["user"];
?>
</body>
</html>
Output:
Page | 27
160450320023
Page | 28
160450320023
Practical-7
Aim: Error handling and exception creating error handling pages with PHP.
Source code:
<?php
//user-defined function with an exception
function checkNumber($num) {
if($num>=1) {
//throw an exception
throw new Exception("Value must be less than 1");
}
return true;
}
//trigger an exception in a "try" block
try {
checkNumber(5);
//If the exception throws, below text will not be display
echo 'If you see this text, the passed value is less than 1';
}
//catch exception
catch (Exception $e) {
echo 'Exception Message: ' .$e->getMessage();
}
?>
Output:
Page | 29
160450320023
<?php
// Creates my error function which prints message
//to user
function myerror($error_no, $error_msg) {
echo "Error: [$error_no] $error_msg ";
echo "\n Now Script will end";
// When error occurred script has to be stopped
die();
}
// Setting set_error_handler
set_error_handler("myerror");
$a = 10;
$b = 0;
// This will generate error
echo($a / $b);;
?>
Output:
Page | 30
160450320023
Page | 31
160450320023
Practical-8
Aim: View the data contained in the My SQL database.
Source code:
<html>
<head>
<title>Assignment question</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
$servername = "localhost";
$username = "root";
$password = " ;
$db = "studentmst";
$con=mysqli_connect($servername, $username, $password, $db);
$rno = $_POST['rno'];
$name = $_POST['name'];
$cno = $_POST['cno'];
$addr = $_POST['addr'];
$brn = $_POST['brn'];
$per = $_POST['per'];
$sql = "INSERT INTO reg(rno, name, cno, addr, brn, per)values($rno,'$name',$cno,'$addr','$brn',$per)";
if(mysqli_query($con,$sql))
{
echo " ;
}
else
{
echo "wrong";
Page | 32
160450320023
}
mysqli_close($con);
}
if(isset($_POST['fetch']))
{
$servername = "localhost";
$username = "root";
$password = " ;
$db = "studentmst";
$con=mysqli_connect($servername, $username, $password, $db);
$sql = "SELECT * FROM reg";
$result = mysqli_query($con,$sql); echo "<table border = '1'>
<tr>
<th>ROLL NO</th>
<th>NAME</th>
<th>CONTACT NUMBER</th>
<th>ADDRESS</th>
<th>BRANCH NAME</th>
<th>PERCENTAGE</th>
</tr>";
while($row= mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>".$row['rno']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['cno']."</td>";
echo "<td>".$row['addr']."</td>";
echo "<td>".$row['brn']."</td>";
echo "<td>".$row['per']."</td>"; echo "</tr>";
}
Page | 33
160450320023
echo "</table>";
}
?>
<form method = "POST", action = " >
<p>ROLL NUMBER</p>
<input type = "text" name = "rno">
<p>Name</p>
<input type = "text" name = "name">
<p>Contact Number</p>
<input type = "text" name = "cno">
<p>Address</p>
<input type = "text" name = "addr">
<p>Branch</p>
<input type = "text" name = "brn">
<p>Percentage</p>
<input type = "text" name = "per"><br>
<input type="submit" name = "submit" value="INSERT"><br>
<input type="submit" name = "fetch" value="FETCH">
</form>
</body>
</html>
Output:
Page | 34
160450320023
Page | 35
160450320023
Practical-9
Aim: Connect to the database from your website.
Source code:
HTML:
<!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>Welcome to travel form</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Anton&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<body>
<img class="bg" src="bg.jpg" alt="background_image">
<div class="container">
<h1>Welcome to Travel Form</h1>
<p>Enter your details and submit this form to confirm your participation in the trip.</p>
<p class="submitMsg">Thanks for your time we wil get back to you soon.</p>
<form action="index.php" method="POST">
<input type="text" name="name" id="name" placeholder="Enter your name">
<input type="text" name="age" id="age" placeholder="Enter your age">
Page | 36
160450320023
<input type="text" name="gender" id="gender" placeholder="Enter your gender">
<input type="email" name="email" id="email" placeholder="Enter your email">
<input type="phone" name="phone" id="phone" placeholder="Enter your phone">
<textarea name="desc" id="desc" cols="30" rows="10" placeholder="Enter any
other information here"></textarea>
<button class="btn">Submit</button>
</form>
</div>
<script src="index.js"></script>
</body>
</html>
PHP:
<?php
$insert = false;
if(isset($_POST['name'])){
$server = "localhost";
$username = "root";
$password = " ;
$con = mysqli_connect($server, $username,
$password); if(!$con){
die("connection is failed due to". mysqli_connect_error());
$name = $_POST['name'];
$gender = $_POST['gender'];
Page | 37
160450320023
$age = $_POST['age'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$desc = $_POST['desc'];
$sql = "INSERT INTO `travelform`.`travelform` (`name`, `age`, `gender`, `email`, `phone`, `desc`,
`dt`)
VALUES ('$name','$age','$gender','$email','$phone','$desc',current_timestamp());";
if($con->query($sql)==true){
$insert = true;
} else {
echo "Error: $sql <br> $con->error";
$con->close();
?>
<!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>Welcome to travel form</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
Page | 38
<link href="https://fonts.googleapis.com/css2?family=Anton&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<body>
<img class="bg" src="bg.jpg" alt="background_image">
<div class="container">
<h1>Welcome to Travel Form</h1>
<p>Enter your details and submit this form to confirm your participation in the trip.</p>
<?php
if($insert == true)
echo "<p class='submitMsg'>Thanks for your time we wil get back to you soon.</p>"
?>
<form action="index.php" method="POST">
<input type="text" name="name" id="name" placeholder="Enter your name">
<input type="text" name="age" id="age" placeholder="Enter your age">
<input type="text" name="gender" id="gender" placeholder="Enter your gender">
<input type="email" name="email" id="email" placeholder="Enter your email">
<input type="phone" name="phone" id="phone" placeholder="Enter your phone">
<textarea name="desc" id="desc" cols="30" rows="10" placeholder="Enter any
other information here"></textarea>
<button class="btn">Submit</button>
</form>
</div>
<script src="index.js"></script>
</body>
</html>
CSS:
*{
margin: 0px;
padding: 0px;
box-sizing: border-box;
font-family: sans-serif;
.container{
max-width: 80%;
/* background-color: rgb(14, 12, 15); */
padding: 34px;
color: whitesmoke;
margin: auto;
.container h1, p{
margin-top: 30px;
text-align: center;
.container h1{
font-size:
35px;
.container p{
font-size: 20px;
input, textarea{
display: block;
font-size: 15px;
width: 80%;
padding: 7px;
margin: 11px
auto;
border: 2px solid
orange; border-radius:
8px; outline: yelow;
form{ displa
y: flex;
align-items: center;
justify-content:
center; flex-direction:
column;
.btn{ color:
black;
background:
yelow; padding:
8px 12px; font-
size: 17px; border:
15px red;
border-radius: 14px;
cursor: pointer;
.bg{
width: 100%;
position: absolute;
z-index: -1;
opacity: 0.9;
.submitMsg{
color: green;
Output:
Practical-10
Aim: Programs to manipulate the table.
Source code:
<?php
$con=mysqli_connect('localhost','root','','cart');
?>
<html>
<head>
<title>Manipulate data From Database</title>
</head>
<body>
<form method="post">
<table>
<tr>
<td>Name</td>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<td>Department</td>
<td>
<select name="dept">
<option value="Computer Engineering">Computer</option>
<option value="Civil Engineering">Civil</option>
<option value="Mechanical Engineering">Mechanical</option>
</select>
</td>
</tr>
<tr>
<td>Semester</td>
<td>
<select name="sem">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</td>
</tr>
<tr>
<td>Gender</td>
<td>
<input type="radio" name="gender" value="male">Male<br>
<input type="radio" name="gender" value="female">Female
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="insert" value="Insert New Student">
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['insert']))
$name=$_POST['name'];
$dept=$_POST['dept'];
$sem=$_POST['sem'];
$gender=$_POST['gender'];
$sql="INSERT INTO
student(Name,Dept,Sem,Gender)
VALUES('$name','$dept','$sem','$gender')";
$query=mysqli_query($con,$sql);
if($query)
echo "Inserted successfuly";
?>
<hr>
<br>
<table border="1">
<tr>
<th>ID</th>
<th>NAME</th>
<th>DEPARTMENT</th>
<th>SEMESTER</th>
<th>GENDER</th>
</tr>
<?php
$sql="SELECT * FROM student";
$query=mysqli_query($con,$sql);
while($raw=mysqli_fetch_assoc($query))
?>
<tr>
<td><?php echo $raw['id'] ?></td>
<td><?php echo $raw['name'] ?></td>
<td><?php echo $raw['dept'] ?></td>
<td><?php echo $raw['sem'] ?></td>
<td><?php echo $raw['gender'] ?></td>
</tr>
<?php
}
?>
</table>
<?php
$sql="SELECT * FROM student";
$query=mysqli_query($con,$sql);
?>
<br>
<hr>
<form method="post">
<select name="id">
<option>Select Student ID</option>
<?php
while($raw=mysqli_fetch_assoc($query))
{?>
<option value="<?php echo $raw['id'] ?>"><?php echo $raw['id'] ?></option>
<?php
?>
</select>
<input type="submit" value="get student" name="sel-id">
<br>
<br>
<?php
if(isset($_POST['sel-id']))
$id=$_POST['id'];
$sql="SELECT * FROM student WHERE id='".$id."'";
$query=mysqli_query($con,$sql);
while($raw=mysqli_fetch_array($query))
{?>
<form method="post">
name : <input type="text" name="name" value="<?php echo $raw['name']; ?>"><br><br>
Department : <input type="text" name="dept" value="<?php echo $raw['dept']; ?>"><br><br>
Semester : <input type="text" name="sem" value="<?php echo $raw['sem']; ?>"><br><br>
<input type="hidden" name="id" value="<?php echo $raw['id']; ?>">
<input type="submit" name="update" value="Update">
<input type="submit" name="delete" value="Delete"><br><br>
</form>
<?php
if(isset($_POST['update']))
$sid=$_POST['id'];
$nm=$_POST['name'];
$dpt=$_POST['dept'];
$sm=$_POST['sem'];
$sql = "UPDATE student SET Name='$nm', Dept='$dpt', Sem='$sm' WHERE student.id='$sid'
LIMIT 1";
$query=mysqli_query($con,$sql);
if($query)
echo "data updated";
header("location:index.php");
elseif(isset($_POST['delete']))
$sid=$_POST['id'];
$sql="DELETE FROM student WHERE id='$sid'";
$delete=mysqli_query($con,$sql);
if($delete)
header("location:index.php");
?>
</body>
</html>
Output: