VL Lab
VL Lab
1
S.N. Name of Experiment Page Date of Remarks
No. Experiment
01. Create a PHP webpage and print “hello 4
world”.
2
08. Write a PHP program to compute 17-18
factorial of a numberusing recursion.
3
Experiment - (1)
Aim:- Create a PHP webpage and print “hello world”.
Source Code:-
<html>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>
Output:-
4
Experiment - (2)
Aim:- Write a PHP program to find maximum of three
numbers.
Source Code:
<head>
<title>Maximum of a three
number</title>
</head>
<body>
<form method="post">
Enter the first number:<br>
<input type="text" name="n1"><br>Enter
the second number:<br>
<input type="text" name="n2"><br>Enter
the third number:<br>
<input type="text" name="n3">
<br>
<input type="submit"
name="submit">
</form>
<?php
if($_POST){
$num1=$_POST["n1"];
$num2=$_POST["n2"];
$num3=$_POST["n3"];
if($num1>$num2 &&
$num1>$num3){
5
echo $num1;
}
else{
if($num2>$num1 &&
$num2>$num3){
echo $num2;
}
Else
echo $num3;
}
}
?>
</body>
</html>
Output:-
6
Experiment - (3)
Source Code:
<head>
<title>Using switch-case</title>
</head>
<body>
<form method="post"> Enter
the number:<br>
<input type="text" name="number">
<input type="submit" name="submit">
</form>
<?php if($_POST){
$var = $_POST['number'];
switch($var)
{
case 1: echo "Hello";
7
break;
case 2: echo "welcome";
break;
}
}
?>
</body>
</html>
Output:-
8
Experiment-(4)
Source Code:
<!DOCTYPE
html>
<html>
<head>
<title>Factorial Program using loopin
PHP</title>
</head>
<body>
<form method="post"> Enter the
Number:<br>
<input type="number"
name="number" id="number">
<input type="submit"
name="submit" value="Submit" />
</form>
<?php
if($_POST){
$fact = 1;
$number = $_POST['number'];echo
"Factorial of
$number:<br><br>";
while($number>=1){
9
$fact = $fact *
$number;
$number--;
}
echo $fact. "<br>";
}
?>
</body>
</html>
Output:-
10
Experiment - (5)
Aim:- Write a program to enter numbers till the user wants.
At the end it should display the count of positive, negative
and zeros entered. (Using do-while loop)
Source Code:
<?php
$Num = 'a';
$count_positive = 0;
$count_negative = 0;
$count_zeroes = 0;do{
$num = readline("Enter a number:");
if($num >0)
$count_positive++;else
if($num<0)
$count_negative++;Else
$count_zeroes++;
$Num = readline("Do you want to
Continue(y/n)?");
}while($Num =='a');
11
echo "\n Total Zeroes entered:",
$count_zeroes;
?>
Output:-
12
Experiment - (6)
Aim:- Write a program that will accept an array of integers asinput,
and output an array where for each item in the source array, the new
array will perform the following operations:
a) For even numbers divide by 2
b) For odd numbers multiply by 3.
Source Code :
<?php
$array = array( 1, 2, 3, 4, 5, 6, 7);
$even_array = [];
$odd_array = [];
for( $i = 0 ; $i < count($array); $i++){if
($array[$i] % 2 == 0){
array_push($even_array, $array[$i] / 2);
$array[$i] = $array[$i] / 2;
}
else{
array_push($odd_array, $array[$i] * 3);
$array[$i] = $array[$i] * 3;
}
13
}
echo "\n";
print_r ($even_array);echo
"\n";
print_r ($odd_array) ; echo "\n
new Array \n";print_r($array);
?
Output:-
14
Experiment - (7)
Aim:- Create an associative array using the countries as keys,
the cities as values and display the data as a table.
Source Code:
<html>
<body>
<?php
$a_array = array("India"=>"New Delhi" , "Afganistan"=>"Kabul" ,
"Australia"=> "Canberra" ,
"Bangladesh"=>"Dhaka" , "Brazil"=>"Brasilia" ,"Canada"=>"Ottawa" ,
"China"=>"Beijing" ,
"France"=>"Paris" , "Japan"=>"Tokyo" ,"Thailand"=>"Bangkok") ;
$j = 0 ;
foreach($a_array as $x => $x_value){
$newarray[0][$j] = $x ;
$newarray[1][$j] = $x_value ;
$j += 1 ;
}
echo "<table border=1>";
echo "<tr><th>Country</th><th>Capital</th></tr>";
15
for($k = 0 ; $k < $j ; $k++){
echo "<tr><td>".$newarray[0][$k]."</td>" ; echo
"<td>".$newarray[1][$k]."</td></tr>" ;
}
echo "</table>" ;
?>
</body>
</html>
Output:-
16
Experiment - (8)
Aim:- Write a PHP program to compute factorial of a number
using recursion.
Source Code:
<?php if(isset($_POST['num']))
$num = $_POST['num'];
function factorial($n) { if($n <=
1)
return 1;
return $n * factorial($n - 1);
}
?>
<html>
<body>
<form action=" <?php $_PHP_SELF ?> " method='post'>
Number : <input type="text" name='num' placeholder='Enterthe
number..'
value="<?php if(isset($num)) echo $num;?>"><br><br>
17
Result : <?php if(isset($num)) echo factorial($num);?>
<br><br>
</form>
</body>
</html>
Output:-
18
Experiment - (9)
Aim:- Write a program that displays a different message
based on time of day. For example, page should display “Good
Morning” if it is accessed in the morning.
Source Code:
<?php
// messages
$morning = "Good morning \n";
$afternoon = "Good afternoon \n";
$evening = "Good evening \n";
$late = "Working late \n";
$Friday = "Get ready for weekend \n";
//get the current time
$time = date('G');
//get the current day
$day = date('I');
//6 AM - 11 AM
if($time >= 6 && $time <= 11)
echo $morning;
//12 PM - 4 PM
19
else if($time >= 12 && $time <= 16)
echo $afternoon;16
//5 PM - 11 PM
else if($time >= 17 && $time <= 24)
echo $evening;
//12 AM - 5 AM
else if($time >= 1 && $time <= 5)
echo $late;
Output :-
20
Experiment - (10)
Aim:- Create pages for signup and sign-in process using PHP
MySQL database operations.
Source Code:
--------------------------SignUp Page Code----------------------------------
<?php
$status = 2;
if(isset($_POST["submitbtn"])){
$email = $_POST['email'];
$name = $_POST['name'];
$Password = $_POST['pass'];
$cpassword = $_POST['cpass'];
$gender = $_POST['gender'];
$con= mysqli_connect("localhost","root","","gec_db");if
(!$con)
die("Server Could Not Connected");if
($Password==$cpassword){
$s="insert into registration values
('".$email."','".$name."','".$Password."','".$gender."')";
$no=mysqli_query($con,$s);
21
if ($no!=0)
$status=1;else
$status=0;
}
else
echo"Password didn't matching!!!";18
}
?>
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method='post'>
<input type="text" name='name' placeholder='Entername'><br><br>
<input type="email" name='email' placeholder='Enter
email'><br><br>
<input type="password" name='pass' placeholder='Enter
password'><br><br>
<input type="password" name='cpass' placeholder='Enter
Confirm
password'><br><br>
22
<input type="radio" name='gender' value='Male'>Male
<input type="radio" name='gender'
value='Female'>Female<br><br>
<input type="submit" name='submitbtn'><br><br>
<?php
if ($status==1)
echo 'register succussfully';else if
($status=0)
echo 'not registed';
?>
</form>
</body>
</html>19
--------------------------SignIn Page Code-----------------------------------
<?php session_start();
$con=mysqli_connect("localhost","root","","gec_db");if(!$con)
die("Server Could Not Connected");if
(isset($_POST['btn'])){
23
$email=$_POST['email'];
$password=$_POST['password'];
$s="select * from registration where EmailID='".$email."' and
Password
='".$password."' ";
$rs=mysqli_query($con,$s);
$no=mysqli_num_rows($rs);
if($no>0){
$_SESSION["username"]=$email;
header("location:userdashboard.php");
}
else
echo "Unable to login";
}
?>
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method='post'>
<input type="email" placeholder='Enter email id'
name='email'><br><br>
24
<input type="password" placeholder='Enter password'
name='password'><br><br>20
<input type="submit" name='btn' value='SignIn'><br><br>
</form>
</body>
</html>
Output:-
25