PHP&MySQL Labprograms (1-18)
PHP&MySQL Labprograms (1-18)
<html>
<head><title>My first program</title></head>
<body>
<h1>To print hello world program</h1>
<?php
echo "hello world program!!! <br>";
print "welcome php language <br>";
printf "welcome php language <br>";
?>
</body>
</html>
Output:
To print hello world program
hello world program!!!
welcome php language
printf "welcome php language
Output:
3. Write a PHP program to find max numbers
<html>
<head><title>largest number</title></head>
<body>
<h1 style="color:green">check the largest number</h1>
<?php
$number1 = 12;
$number2 = 7;
$number3= 1;
$maxNumber = [$number1, $number2, $number3];
sort($maxNumber);
echo "The largest number among three is:
$maxNumber[2]\n";
?>
</body>
</html>
Output:
4.Write a PHP program to swap 2 numbers
<html>
<head><title>lab program4</title></head>
<body>
<h3>To swap 2 numbers</h3>
<?php
$a=30;
$b=27;
$temp=$a;
$a=$b;
$b=$temp;
Output:
5.Write a PHP program to find factorial of numbers
<html>
<head><title>factorial of a number</title></head>
<body>
<?php
function factorial($num)
{
if($num<=1)
{
return 1;
}
else
{
return $num*factorial($num-1);
}
}
$num=3;
$fact=factorial($num); echo
"factorial of $num is $fact"
?>
</body>
</html>
Output:
6.Write a program to check number is palindrome
<?php
$n=110;
$rev=0;
$temp=$n;
while(floor($n))
{
$rem=$n%10;
$rev=$rev*10+$rem;
$n=$n/10;
}
if($temp==$rev) echo
"$temp is palindrome"; else
echo "$temp is not a palindrome";
?>
Output:
7.Write a PHP program to reverse number
<?php
$num=12;
$t=$num;
$sum=0;
$rem=0;
$rev=0;
for($i=0;$i<=strlen($num);$i++)
{
$rem=$num%10;
$rev=$rev*10+$rem;
$sum=$sum+$rem;
$num=$num/10;
}
echo "The user defined number is:$t",“<br>”;
echo "Rev the number $sum of digits is: $sum";
?>
8.Write a PHP script to generate a Fibonacci series.
<?php
$num=0;
$n1=0;
$n2=1;
echo "Fibonacci series for first 7 num:";
echo "\n";
echo $n1.' ', $n2.' ';
while($num<5)
{
$n3=$n2+$n1;
echo $n3.' ';
$n1=$n2;
$n2=$n3;
$num=$num+1;
}
?>
Output:
9.Write a PHP script to implement atleast seven string functions
<?php
$str1="My name is MAHITH";
$str1= strtolower($str1); echo
"LOWER CASE:\t";
echo$str1, "<br>";
$str3 = "mahith";
$str3 = ucfirst($str3);
echo "Converting first character into uppercase:\t"; echo
$str3 . "<br>";
$str4 = "MAHITH";
$str4 = lcfirst($str4);
echo "Converting first character into lowercase:\t";
echo $str4 . "<br>";
<?php
$arr=[1,2,3,5];
$pos=3; $val=4;
array_splice($arr,$pos,0,$val);
print_r($arr);
?>
Output:
11.Write a PHP script to implement constructor and destructor
<?php
class Fruit {
public $name;
public $color;
function construct($name, $color) {
//the above line need to use function keyword space double underscore same for
destruct() also.)
$this->name = $name;
$this->color = $color;
}
function destruct() {
echo "The fruit is {$this->name} and the color is {$this->color}";
}
}
$apple = new Fruit("Apple", "Red");
?>
Output:
12.Write a PHP script to implement form handling using get method
Formget.html (this is a file name you used to save in your folder)
<!DOCTYPE html>
<html>
<body>
<form action="form_get.php" method="GET">
Name: <input type="text" name="name"/><br>
Age: <input type="text" name="age"/><br>
<input type="submit"/>
</form>
</body>
</html>
Output:
13.Write a PHP script to implement form handling using post
method Formpost.html
<!DOCTYPE html>
<html>
<body>
<form action="form_POST.php" method="POST"> Name: <input type="text"
name="name"/><br> Age: <input type="text" name="age"/><br>
<input type="submit"/>
</form>
</body>
</html>
Form_post.php
<!DOCTYPE html>
<html>
<body>
WELCOME <?php echo
$_POST["name"];?><br> Your Age is:<?php
echo $_POST["age"];?><br>
</body>
</html>
Output:
14.Write a PHP script to show the functionality of date and time function
<?php
date_default_timezone_set('UTC');
echo"The current date and time is " . date("Y-m-d H:i:s") ."<br>";
echo"The current year is ". date("Y") ."<br>";
echo"The current month is ". date("F") ."<br>";
echo"The current day of the week is ". date("l") ."<br>";
echo"The current time is ". date("h:i:s A")."<br>";
echo"The current timestamp is ". time() ."<br>";
?>
Output:
15. 15. Write a PHP program that receive string as a form input.
<html>
<head>
<title>php script that receive string as a form input</title> </head>
<body>
<form method='POST'>
<h2>ENTER YOUR NAME:</h2>
<input type ="text" name="name"><br/>
<input type="submit" value="Submit">
</form>
<?php
$name=$_POST["name"];
echo "<h3>Hello $name</h3>";
?>
</body>
</html>
OUTPUT:
Hello josephine
16. Write a PHP Script to implement database Creation
<?php
$servername="localhost";
$username="root";
$password="";
$conn=new mysqli($servername,$username,$password);
if($conn->connect_error)
{
die("connection failed:".$conn->connect_error);
}
$sql="CREATE DATABASE swedb";
if($conn->query($sql)==true)
{
echo "Database created successfully with the name swedb";
}
else
{
echo "Error creating database:".$conn->error;
}
$conn->close();
?>
OUTPUT:
Database created successfully with the name newdb
17.Write a PHP Script to Create table.
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="newdb";
$conn=new mysqli($servername,$username,$password,$dbname);
if($conn->connect_error)
{
die("connection failed:".$conn->connect_error);
}
$sql="CREATE TABLE employees(id int(2) PRIMARY KEY,firstname
VARCHAR(30)NOT NULL,
lastname VARCHAR(30)NOT NULL,
email VARCHAR(50) )";
if($conn->query($sql)==true)
{
echo "table employeees created successfully";
}
else
{
echo "Error creating table:".$conn->error;
}
$conn->close();
?>
OUTPUT:
table employees created successfully
18.Develop a PHP program to design a college admission formusing
MySQL Database.
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="newdb";
$conn=new mysqli($servername,$username,$password,$dbname);
if($conn->connect_error)
{
die("connection failed:".$conn->connect_error);
}
if($conn->query($sql)==true)
{
echo "table college created successfully";
}
else
{
echo "Error creating table:".$conn->error;
}
$conn->close();
?>
OUTPUT: