Steps to write php code and see Output
1. Open XAMPP
2. Start Apache and MySQL
3. Open text editors like Sublime , Notepad++ etc
4. Save -> Local disk(c) -> XAMPP -> htdocs -> filename.php
5. Type the code and save it
6. Click web browser like chrome type localhost/filename.php 7. Press Enter key
PHP Program List
Write Q1 to Q15 for php Lab Work.
1. Write a PHP program to find the greatest number among three numbers.
<!DOCTYPE html>
<html>
<body>
<form method="post">
<input type="text" name="firstNum" placeholder="first number">
<input type="text" name="secondNum"
placeholder="second number">
<input type="text" name="thirdNum"
placeholder="third number">
<input type="submit">
</form>
<?php
@$num1=$_REQUEST['firstNum'];
@$num2=$_REQUEST['secondNum'];
@$num3=$_REQUEST['thirdNum'];
if($num1>$num2&&$num1>$num3)
echo " Greatest number = ".$num1;
else if($num2>$num1&&$num2>$num3)
echo " Greatest number = ".$num2;
else
echo " Greatest number = ".$num3;
?>
</body>
</html>
2. Write a PHP program to find the smallest number among three numbers.
<!DOCTYPE html>
<html>
<body>
<form method="post">
<input type="text" name="firstNum" placeholder="first number">
<input type="text" name="secondNum"
placeholder="second number">
<input type="text" name="thirdNum"
placeholder="third number">
<input type="submit">
</form>
<?php
@$num1=$_REQUEST['firstNum'];
@$num2=$_REQUEST['secondNum'];
@$num3=$_REQUEST['thirdNum'];
if($num1<$num2&&$num1<$num3)
echo " Smallest number = ".$num1;
else if($num2<$num1&&$num2<$num3)
echo " Smallest number = ".$num2;
else
echo " Smallest number = ".$num3;
?>
</body>
</html>
3. Write a PHP program to swap two numbers.
<!DOCTYPE html>
<html>
<body>
<form method="post">
<input type="text" name="firstNum" placeholder="first number">
<input type="text" name="secondNum" placeholder="second number">
<input type="submit">
</form>
<?php
@$num1=$_REQUEST['firstNum'];
@$num2=$_REQUEST['secondNum'];
echo "Values before swapping ";
echo " First Number = ".$num1;
echo " Second number = ".$num2;
$temp=$num1;
$num1=$num2;
$num2=$temp;
echo "Values after swapping ";
echo " First Number = ".$num1;
echo " Second number = ".$num2;
?>
</body>
</html>
4. Write a PHP program to input name and address using form and display them.
<!DOCTYPE html>
<html>
<body>
<form method="post">
<input type="text" name="username" placeholder="Enter name">
<input type="text" name="useraddress" placeholder="Enter address">
<input type="submit">
</form>
<?php
@$uname=$_REQUEST['username'];
@$uaddress=$_REQUEST['useraddress'];
echo " NAME = ".$uname;
echo " ADDRESS = ".$uaddress;
?>
</body>
</html>
5. Write a PHP program to input a string through a text box and find its length.
<!DOCTYPE html>
<html>
<body>
<form method="post">
<input type="text" name="userText" placeholder="Enter text">
<input type="submit">
</form>
<?php
$str=$_REQUEST["userText"];
echo "Length=".strlen($str);
?>
</body>
</html>
6. Write a PHP program to store some strings in an array and print them.
<?php
$subjects = array("Compulsory English", "Compulsory Nepali", "Social","Accounts","Economics","Computer");
for ($i=0;$i<count($subjects);$i++)
{
echo $subjects[$i];
echo "<br>";
}
?>
7. Write a PHP program to sort data stored in an array.[Use sort() function and print_r() to print.]
<!DOCTYPE html>
<html>
<body>
<?php
$numbers = array(8,4,5,3,1);
sort($numbers);
print_r($numbers);
?>
</body>
</html>
Output
Array ( [0] => 1 [1] => 3 [2] => 4 [3] => 5 [4] => 8 )
8. Write a PHP program to know whether two strings are the same or not.
<!DOCTYPE html>
<html>
<head>
<title>Compare two string</title>
</head>
<body>
<?php
$str1 = "Alex Lal Karn";
$str2 = "Computer Science";
if(strcmp($str1, $str2) == 0){
echo "Strings are same";
}else{
echo "Strings are not same";
}
?>
</body>
</html>
9. Write a php program to display your name 100 times.
<!DOCTYPE html>
<html>
<head>
<title>Display name 100 times</title>
</head>
<body>
<?php
$name = "Alex Lal Karn";
for($i=0;$i<100;$i++) {
echo $name;
echo "<br>";
}
?>
</body>
</html>
10. Write a PHP program to print a multiplication table of a number. Input the number via form.
<!DOCTYPE html>
<html>
<body>
<form method="post">
<input type="text" name="num" placeholder="Enter number">
<input type="submit">
</form>
<?php
$n=$_REQUEST["num"];
for($i=1;$i<=10;$i++){
echo $n*$i;
echo '<br>';
}
?>
</body>
</html>
11. Write a php program to calculate factorial of input number.
<!DOCTYPE html>
<html>
<body>
<form method="post">
<input type="text" name="num" placeholder="Enter number">
<input type="submit">
</form>
<?php
$n=$_REQUEST["num"];
$f=1;
for($i=1;$i<=$n;$i++){
$f=$f*$i;
}
echo "Factorial = ".$f;
?>
</body>
</html>
12. Write a PHP program to connect a database named "student".
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="student";
$conn=new mysqli($servername,$username,$password,$dbname);
if($conn->connect_error)
die("Connection failed:".mysqli_connect_error());
else
echo "Database connected successfully";
?>
13. Write a PHP program to insert a record in a database named student with fields id,name and grade.
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="student";
$conn=new mysqli($servername,$username,$password,$dbname);
if($conn->connect_error)
die("Connection failed:".mysqli_connect_error());
else
echo "Database connected successfully";
$sql="insert into studentrec (id,name,grade)values (101,'Tsering Sherpa',12)";
if($conn->query($sql)===true)
{
echo "Inserted data successfully";
}
else{
echo "Error in inserting data:".$conn->error;
}
$conn->close();
?>
14. Write a PHP program to display all records(previous question) of students who are in grade 11.
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="student";
$conn=new mysqli($servername,$username,$password,$dbname);
if($conn->connect_error){
die("Connection failed:".mysqli_connect_error());
}
$sql="SELECT *FROM studentrec WHERE grade='11'";
$result=$conn->query($sql);
if($result->num_rows>0)
{
while($row=$result->fetch_assoc())
{
echo "<br> id:".$row["id"]." - Name: ".$row["name"]." - Grade : ".$row["grade"];
}
}
else {
echo "0 results";
}
$conn->close();
?>
15. Write a PHP program to delete records of a student whose id is 3.
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="student";
$conn=new mysqli($servername,$username,$password,$dbname);
if($conn->connect_error)
die("Connection failed:".mysqli_connect_error());
$sql="DELETE FROM studentrec WHERE id=3";
if($conn->query($sql)===true)
{
echo "Data deleted ";
}
else{
echo "Error in deleting data:".$conn->error;
}
$conn->close();
?>