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>
[mks_button size="medium" title="View Demo" style="squared" url="https://phpgurukul.com/programs/addtwonumbers.php" target="_blank" bg_color="#dd3333" txt_color="#FFFFFF" icon="fa-play" icon_type="fa" nofollow="0"]

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>
[mks_button size="medium" title="View Demo" style="squared" url="https://phpgurukul.com/programs/swaptwonumbers.php" target="_blank" bg_color="#dd3333" txt_color="#FFFFFF" icon="fa-play" icon_type="fa" nofollow="0"]

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>
[mks_button size="medium" title="View Demo" style="squared" url="https://phpgurukul.com/programs/evenoddnumber.php" target="_blank" bg_color="#dd3333" txt_color="#FFFFFF" icon="fa-play" icon_type="fa" nofollow="0"]

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>
[mks_button size="medium" title="View Demo" style="squared" url="https://phpgurukul.com/programs/tablenumber.php" target="_blank" bg_color="#dd3333" txt_color="#FFFFFF" icon="fa-play" icon_type="fa" nofollow="0"]

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>
[mks_button size="medium" title="View Demo" style="squared" url="https://phpgurukul.com/programs/factorialnumber.php" target="_blank" bg_color="#dd3333" txt_color="#FFFFFF" icon="fa-play" icon_type="fa" nofollow="0"]

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>
[mks_button size="medium" title="View Demo" style="squared" url="https://phpgurukul.com/programs/armstrongnumber.php" target="_blank" bg_color="#dd3333" txt_color="#FFFFFF" icon="fa-play" icon_type="fa" nofollow="0"]

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>
[mks_button size="medium" title="View Demo" style="squared" url="https://phpgurukul.com/programs/primenumber.ph" target="_blank" bg_color="#dd3333" txt_color="#FFFFFF" icon="fa-play" icon_type="fa" nofollow="0"]

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>
[mks_button size="medium" title="View Demo" style="squared" url="https://phpgurukul.com/programs/fibonacciseries.php" target="_blank" bg_color="#dd3333" txt_color="#FFFFFF" icon="fa-play" icon_type="fa" nofollow="0"]

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>
[mks_button size="medium" title="View Demo" style="squared" url="http://phpgurukul.com/programs/reversenumber.php" target="_blank" bg_color="#dd3333" txt_color="#FFFFFF" icon="fa-play" icon_type="fa" nofollow="0"]

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>
[mks_button size="medium" title="View Demo" style="squared" url="https://phpgurukul.com/programs/reversestring.php" target="_blank" bg_color="#dd3333" txt_color="#FFFFFF" icon="fa-play" icon_type="fa" nofollow="0"]]]>

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.

Follow us

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