PHPGurukul

Basic PHP Programs

Add two Numbers

?php
if(isset($_POST['submit']))
{
$num1=$_POST['num1'];
$num2=$_POST['num2'];
$result=$num1+$num2;
echo "Sum of " .$num1. " and ".$num2. " is " .$result;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Add two number</title>
</head>
<body>
<table>
<form name="add" method="post">
<tr>
<td>Number 1 :</td>
<td><input type="text" name="num1" required></td>
</tr>
<tr>
<td>Number 2 :</td>
<td><input type="text" name="num2" required></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Add" name="submit" /></td>
</tr>
</form>
</table>
</body>
</html>
View Demo

Swap Two Numbers

<?php
if(isset($_POST['submit']))
{
echo "<b>Number before Swapping</b><br>";
$num1=$_POST['num1'];
$num2=$_POST['num2'];
echo "Number one is $num1 <br />";
echo "Number two is $num2 <br />";
$temp=$num1;
$num1=$num2;
$num2=$temp;
echo "<b>Number after Swapping</b> <br />";
echo "Number one is $num1<br />";
echo "Number two is $num2<br />";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Swap Two Numbers</title>
</head>
<body>
<table>
<form name="swap" method="post">
<tr>
<td>Number 1 :</td>
<td><input type="text" name="num1" required></td>
</tr>
<tr>
<td>Number 2 :</td>
<td><input type="text" name="num2" required></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="swap" name="submit" /></td>
</tr>
</form>
</table>
</body>
</html>
View Demo

Even odd Number Program

<?php
if(isset($_POST['submit']))
{
$num=$_POST['num'];
if($num%2==0){
  echo "$num  is a even number ";
}
else
{
echo "$num is a odd number";
}
}
?>
<!DOCTYPE html>
<html>
<head>
	<title>Even odd  Numbers</title>
</head>
<body>
<table>
<form name="evenodd" method="post">
<tr>
<td>Enter Number :</td>
<td><input type="text" name="num" required></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Check" name="submit" /></td>
</tr>
</form>
</table>
</body>
</html>
View Demo

Print Table of any Number

?php
if(isset($_POST['submit']))
{
$num=$_POST['num'];
define('NUM',$num);
for($i=1 ; $i<=10 ; $i++)
{
echo $i*NUM;
echo '<br>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Table of a Number</title>
</head>
<body>
<table>
<form name="table" method="post">
<tr>
<td>Enter Number :</td>
<td><input type="text" name="num" required></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="submit" name="submit" /></td>
</tr>
</form>
</table>
</body>
</html>
View Demo

Factorial of a Number

<?php
if(isset($_POST['submit']))
{
$num=$_POST['num'];
$fact = 1;
for ($i=$num; $i>=1; $i--)
{
 $fact = $fact * $i;
}
echo "factorial of  $num is  $fact";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Factorial of a Number</title>
</head>
<body>
<table>
<form name="factorial" method="post">
<tr>
<td>Enter Number :</td>
<td><input type="text" name="num" required></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Check" name="submit" /></td>
</tr>
</form>
</table>
</body>
</html>
View Demo

Armstrong Program Number in PHP

<?php
if(isset($_POST['submit']))
{
$num=$_POST['num'];
$sum=0;
$temp=$num;
while($temp!=0)
{
$rem=$temp%10;
$sum=$sum+$rem*$rem*$rem;
$temp=$temp/10;
}
if($num==$sum)
{
echo "$num is an Armstrong number";
}
else
{
echo "$num is not an Armstrong number";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Armstrong Number</title>
</head>
<body>
<table>
<form name="armstrong" method="post">
<tr>
<td>Enter Number :</td>
<td><input type="text" name="num" required></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Check" name="submit" /></td>
</tr>
</form>
</table>
</body>
</html>
View Demo

Prime Number Program

<?php
if(isset($_POST['submit']))
{
$num=$_POST['num'];
for($j=2;$j<=$num;$j++ )
{
for($k=2;$k<$j;$k++ )
{
if($j%$k==0)
{
break;
}
}
if( $k == $j )
echo "Prime Number : $j <br />";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Prime Number</title>
</head>
<body>
<table>
<form name="prime" method="post">
<tr>
<td>Enter Number :</td>
<td><input type="text" name="num" required></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Check" name="submit" /></td>
</tr>
</form>
</table>
</body>
</html>
View Demo

Fibonacci Series Program

<?php
if(isset($_POST['submit']))
{
	$n=$_POST['num'];
define('NUM',$n);
$a = 0;
$b = 1;
echo "$a $b "; // 0 1
for($i=1   ; $i<= NUM-2 ;  $a=$b, $b=$c, $i++ )
{
  echo $c = $a+$b;
  echo " ";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Fibonacci Series </title>
</head>
<body>
<table>
<form name="fibonacci" method="post">
<tr>
<td colspan="2">Enter number upto you want to print the series.
</tr>
<tr>
<td>Enter Number :</td>
<td><input type="text" name="num" required></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Check" name="submit" /></td>
</tr>
</form>
</table>
</body>
</html>
View Demo

Reverse Number Program

<?php
if(isset($_POST['submit']))
{
$n=$_POST['num'];
function reverse_number($number)
{
$snum = (string) $number;
$revstr = strrev($snum);
$reverse = (int) $revstr;
return $reverse;
}
echo reverse_number($n);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Reverse a number </title>
</head>
<body>
<table>
<form name="reverse" method="post">
<tr>
<td>Enter Number :</td>
<td><input type="text" name="num" required></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Reverse" name="submit" /></td>
</tr>
</form>
</table>
</body>
</html>
View Demo

Reverse String Program

<?php
if(isset($_POST['submit']))
{
$string=$_POST['string'];
$length = strlen($string);
for ($i=($length-1) ; $i >= 0 ; $i--)
{
  echo $string[$i];
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Reverse a String</title>
</head>
<body>
<table>
<form name="reversestring" method="post">
<tr>
<td>Enter a String  :</td>
<td><input type="text" name="string" required></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Reverse" name="submit" /></td>
</tr>
</form>
</table>
</body>
</html>
View Demo

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.
.

Recommended Tutorials for you


Add comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Web Hosting with a FREE domain. On sale ₹99.00/mo

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.