PHP LAB
1. Develop a PHP program to display prime numbers between the given ranges
and display the total number of prime numbers.
<?php
$number=1;
while($number <50)
{
$div_count=0;
for ($i=1;$i<=$number;$i++)
{
if(($number%$i)==0)
{
$div_count++;
}
}
if($div_count<3)
{
echo $number." , ";
}
$number=$number+1;
}
?>
*************************OUTPUT*********************************
SSMS BCA College, Athani Page 1
PHP LAB
2. Develop a PHP program and check message passing mechanism between pages.
form1.php
<html>
<body>
<form action="welcome1.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
welcome1.php
<html>
<body>
Welcome <?php echo $_POST["fname"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old!
</body>
</html>
*******************OUTPUT***********************
SSMS BCA College, Athani Page 2
PHP LAB
3. Write a PHP program to implement simple calculator operations.
<html>
<body>
<form method="post">
<input type="text" name="num1"/><br>
<input type="text" name="num2"/><br>
<input type="submit" name="submit" value="calculate"/><br>
</form>
<?php
if(isset($_POST['submit']))
{
$a=$_POST['num1'];
$b=$_POST['num2'];
echo "Addition=".($a+$b)."<br>";
echo "Substraction=".($a-$b)."<br>";
echo "Multiplication=".($a*$b)."<br>";
echo "Division=".($a/$b)."<br>";
}
?>
</body>
</html>
**************************OUTPUT***************************
SSMS BCA College, Athani Page 3
PHP LAB
4. Develop a PHP program to demonstrate String functions.(any 6)
<html>
<body>
<?php
echo strlen("Welcome to Cloudways");
echo "<br>";
echo str_word_count("Welcome to Cloudways");
echo "<br>";
echo strrev("Welcome to Cloudways");
echo "<br>";
echo ucwords("welcome to the php world");
echo "<br>";
echo strtoupper("welcome to cloudways");
echo "<br>";
echo strtolower("WELCOME TO CLOUDWAYS");
echo "<br>";
echo str_repeat("=",13);
echo "<br>";
?>
</body>
</html>
*******************OUTPUT***********************
SSMS BCA College, Athani Page 4
PHP LAB
5.Write a PHP program to illustrate built in array manipulation functions.(Any 6)
<html>
<head>
<title> array manipulation function</title>
</head>
<body>
<?php
$a=array("red","green","blue");
array_pop($a);
print_r($a);
?>
<br/>
<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>
<br/>
<?php
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
?>
<br/>
<?php
$a=array(5,15,25);
echo array_sum($a);
SSMS BCA College, Athani Page 5
PHP LAB
?>
<br/>
<?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>
<br/>
<?php
$cars=array("Volvo","BMW","Toyota");
echo sizeof($cars);
?>
</body>
</html>
*******************OUTPUT***********************
SSMS BCA College, Athani Page 6
PHP LAB
6.Write a PHP 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
<?php
date_default_timezone_set("Asia/Kolkata");
$h=date('G');
if($h>=5 && $h<=11)
{
echo "good morning";
}
else if($h>=12 && $h<=15)
{
echo "good afternoon";
}
else if($h>=18 && $h<=21)
{
echo "good evening";
}
else
{
echo "good night";
}
?>
*******************OUTPUT***********************
SSMS BCA College, Athani Page 7
PHP LAB
7.Write a PHP program that accepts two numbers using a web form and calculates
greatest common divisor (GCD) and least common multiple (LCM) of entered
numbers.(Use recursive function)
<?php
function getGCD($a, $b)
{
if ($b == 0)
{
return $a;
}
Else
{
return getGCD($b, $a % $b);
}
}
function getLCM($a, $b)
{
return ($a * $b) / getGCD($a, $b);
}
?>
<?php
if (!isset($_POST['submit']))
{
?>
<form method="post" action="gcf_lcm.php">
Enter two integers: <br />
<input type="text" name="num_1" size="4" />
<p>
<input type="text" name="num_2" size="4" />
SSMS BCA College, Athani Page 8
PHP LAB
<p>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
}
else
{
$num1 = (int)$_POST['num_1'];
$num2 = (int)$_POST['num_2'];
echo "You entered: $num1, $num2";
echo "<br />";
echo "The GCD of ($num1, $num2) is: " . getGCD($num1, $num2);
echo "<br />";
echo "The LCM of ($num1, $num2) is: " . getLCM($num1, $num2);
}
?>
</body>
</html>
*******************OUTPUT***********************
SSMS BCA College, Athani Page 9
PHP LAB
8. Develop a PHP program to demonstrate constructors and destructors.
<?php
class Machine
{
function construct()
{
echo "Starting up...\n";
}
function destruct()
{
echo "Shutting down...\n";
}
}
$m = new Machine();
unset($m);
?>
*******************OUTPUT***********************
SSMS BCA College, Athani Page 10
PHP LAB
9. Develop a PHP code to read the values entered into the form using the MySQL database.
Connection.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Registration.php
<?php
if(isset($_POST['register']))
{
include 'index.php';
$name = $_POST['name'];
$un = $_POST['username'];
$email = $_POST['email'];
$pass = $_POST['password'];
$conpass = $_POST['conpassword'] ;
$query = "INSERT INTO registration(name, username, email, Password, ConPassword) VALUES
('$name', '$un','$email', '$pass', '$conpass')";
$result = mysqli_query($conn, $query);
if($result){
echo '<script type="text/javascript">';
echo ' alert("DATA IS SAVED")'; //not showing an alert box.
echo '</script>';
}
else{
echo '<script type="text/javascript">';
echo ' alert("DATA NOT INSERTED")'; //not showing an alert box.
echo '</script>';
}
SSMS BCA College, Athani Page 11
PHP LAB
mysqli_close($conn);
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-
Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous">
</head>
<body>
<h2>HTML Forms</h2>
<form name="registration" method="post" style="margin: 150px;margin-top: 0px;border:2px dashed
gray;padding: 10px;">
<div class="form-group">
<label for="exampleInputEmail1">Name</label>
<input type="text" class="form-control" id="name"name="name" placeholder="Enter name">
</div>
<div class="form-group">
<label for="exampleInputEmail1">User Name</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Enter
name">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" name="email" aria-describedby="emailHelp"
placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control"name="password" id="exampleInputPassword1"
placeholder="Password">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Cofirm Password</label>
<input type="password" class="form-control"name="conpassword" id="exampleInputPassword1"
placeholder="Password">
</div>
<button type="submit" name="register" class="btn btn-primary">Submit</button>
</form>
<table class="table">
SSMS BCA College, Athani Page 12
PHP LAB
<thead>
<tr>
<th scope="col">name</th>
<th scope="col">username</th>
<th scope="col">email</th>
<th scope="col">password</th>
<th scope="col">delete</th>
<th scope="col">update</th>
</tr>
</thead>
<tbody>
<?php
include 'index.php';
$query = "SELECT * FROM registration";
$result = mysqli_query($conn, $query);
while($rows = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['username']; ?></td>
<td><?php echo $rows['email']; ?></td>
<td><?php echo $rows['Password']; ?></td>
<td><a href='crud.php?del_user=<?php echo $rows['name']; ?>'><button type="submit"
class="btn btn-danger btn-sm" style="font-size: 12px;" style="border-
radius:50px">DELETE</button></a></td>
<td><a href='update.php?update_user=<?php echo $rows['name']; ?>'><button type="submit"
class="btn btn-danger btn-sm" style="font-size: 12px;" style="border-
radius:50px">update</button></a></td>
</tr>
<?php
}
?>
</body>
</html>
CRUD.php
<?php
include 'index.php';
if(isset($_GET['del_user']))
{
$id = $_GET['del_user'];
$query = "DELETE FROM registration WHERE name= '$id'";
SSMS BCA College, Athani Page 13
PHP LAB
$result = mysqli_query($conn, $query);
echo "<meta http-equiv='refresh' content='0;url=reg.php'>";
}
?>
*******************OUTPUT***********************
SSMS BCA College, Athani Page 14
PHP LAB
10. Create Laravel Project using composer display Simple “Hello World” Message on Web page.
routes\web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/helloview', function () {
return view('hello');
});
resources\views\hello.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
SSMS BCA College, Athani Page 15
PHP LAB
*******************OUTPUT***********************
SSMS BCA College, Athani Page 16