Basic PHP Programs
How to Add Two Numbers using PHP?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
?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> |
How Swap Two Numbers in PHP?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?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> |
How find Even odd Numbers in PHP?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?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> |
How to Print Table of any Number in PHP?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
?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> |
How to find the Factorial of a Number in PHP?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?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> |
How to find an Armstrong Program Number in PHP?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?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> |
How to find the Prime Number using PHP?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?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> |
How to print Fibonacci Series using PHP?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?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> |
How to Reverse Number using PHP?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?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> |
How to Reverse a String using PHP?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?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> |