You are on page 1of 16

PHP Program To Print Sum Of Digits

<!DOCTYPE html>
<html>
<body>
 
<?php
$txt1 = "Sum of x and y is:";
$x = 1234;
$y = 4321;
 
echo "<h3>" . $txt1 . "</h3>";
echo $x + $y;
?>
 
</body>
</html>
Output
Sum of x and y is:
5555

PHP Program To Check Prime Number


<!DOCTYPE html>
<html>
<body>
 
<?php
$num = 13;
$count=0;
for ( $i=1; $i<=$num; $i++)
{
if (($num%$i)==0)
{
$count++;
}
}
if ($count<3)
{
echo "$num is a prime number.";
}
else
{
echo "$num is not a prime number.";
}
?>
 
</body>
</html>
Output
13 is a prime number.

PHP Program To Print Table Of A Number


<!DOCTYPE html>
<html>
<body>
 
<?php
$num = 9;
for($i=1; $i<=10; $i++)
{
$product = $i*$num;
echo "$num * $i = $product" ;
echo '<br>';
}
?>
 
</body>
</html>
Output
9*1=9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90

PHP Program To Print Factorial Of A Number


<!DOCTYPE html>
<html>
<body>
 
<?php
$n = 10;
$f = 1;
for ($i=$n; $i>=1; $i--)
{
$f = $f * $i;
}
echo "$n! = $f";
?>
 
</body>
</html>
Output
10! = 3628800

PHP Program To Check For An Armstrong Number


<!DOCTYPE html>
<html>
<body>
 
<?php
 
$n=371;
$sum=0;
$i=$n;
 
while($i!=0)
{
$x=$i%10;
$sum=$sum+$x*$x*$x;
$i=$i/10;
}
 
if($n==$sum)
{
echo "$n is an Armstrong Number.";
}
 
else
{
echo "$n is not an Armstrong Number.";
}
?>
 
</body>
</html>
Output
371 is an Armstrong Number.

PHP Program To Check For A Palindrome Number


<!DOCTYPE html>
<html>
<body>
 
<?php
 
$num = 123454321;
$x = 0;
$n =$num;
 
while(floor($num))
{
$mod = $num%10;
$x = $x * 10 + $mod;
$num = $num/10;
}
 
if($n==$x)
{
echo "$n is a Palindrome number.";
}
 
else
{
echo "$n is not a Palindrome number.";
}
?>
 
</body>
</html>
Output
123454321 is a Palindrome number.

PHP Program To Print Fibonacci Series


Without using recursion:
Example
<!DOCTYPE html>
<html>
<body>
 
<?php
$n = 0;
$a = 0;
$b = 2;
echo "Fibonacci series with the first 2 numbers as 0 and 2 is: ";
echo "$a, $b";
 
while ($n < 26 )
{
$c = $b + $a;
echo ", ";
echo "$c";
$a = $b;
$b = $c;
$n = $n + 1;
}
?>
 
</body>
</html>
Output
Fibonacci series with the first 2 numbers as 0 and 2 is: 0, 2, 2,
4, 6, 10, 16, 26, 42, 68, 110, 178, 288, 466, 754, 1220, 1974,
3194, 5168, 8362, 13530, 21892, 35422, 57314, 92736, 150050,
242786, 392836

Using recursion:
Example
<!DOCTYPE html>
<html>
<body>
 
<?php
 
function fibonacci($x,$y)
{
$c = $x + $y;
return $c;
}
 
$a = 0;
$b = 2;
echo "Fibonacci series with the first 2 numbers as 0 and 2 is: ";
echo "$a, $b";
 
for ($i = 0; $i < 26; $i++)
{
echo ", ";
echo fibonacci($a,$b);
$z = fibonacci($a,$b);
$a = $b;
$b = $z;
 
}
?>
 
</body>
</html>
Output
Fibonacci series with the first 2 numbers as 0 and 2 is:
0, 2, 2, 4, 6, 10, 16, 26, 42, 68, 110, 178, 288, 466, 754, 1220,
1974, 3194, 5168, 8362, 13530, 21892, 35422, 57314, 92736, 150050,
242786, 392836

PHP Program To Reverse A Number


<!DOCTYPE html>
<html>
<body>
 
<?php
 
$num = 123456789;
$x = 0;
$n =$num;
 
while(floor($num))
{
$mod = $num%10;
$x = $x * 10 + $mod;
$num = $num/10;
}
echo "Reverse of $n is $x.";
?>
 
</body>
</html>
Output
Reverse of 123456789 is 987654321.

PHP Program To Reverse A String


<!DOCTYPE html>
<html>
<body>
 
<?php
$string = "MIRROR IMAGE";
echo "$string : " .strrev ( $string );
?>
 
</body>
</html>
Output
MIRROR IMAGE : EGAMI RORRIM

PHP Program To Swap Two Numbers


Using Third Variable:
Example
<!DOCTYPE html>
<html>
<body>
 
<?php
 
echo "Before Swapping:<br>";
$a = 1;
$b = 2;
echo "a = $a<br>";
echo "b = $b<br><br>";
 
echo "After swapping:<br>";
$temp = $a;
$a = $b;
$b = $temp;
 
echo "a = $a<br>";
echo "b = $b<br>";
?>
 
</body>
</html>
Output
Before Swapping:
a=1
b=2
 
After swapping:
a=2
b=1
Without Using Third Variable:
Example
<!DOCTYPE html>
<html>
<body>
 
<?php
echo "Before Swapping:<br>";
$a = 1;
$b = 2;
echo "a = $a<br>";
echo "b = $b<br><br>";
 
echo "After swapping:<br>";
 
$a = $a + $b;
$b = $a - $b;
$a = $a - $b;
 
echo "a = $a<br>";
echo "b = $b";
?>
 
</body>
</html>
Output
Before Swapping:
a=1
b=2
 
After swapping:
a=2
b=1

PHP Program To Check Leap Year


Example
<!DOCTYPE html>
<html>
<body>
 
<?php
$year = 2032;
 
if((0 == $year % 4) & (0 != $year % 100) | (0 == $year % 400))
{
echo "$year is a Leap Year.";
}
 
else
{
echo "$year is not a Leap Year.";
}
?>
 
</body>
</html>
Output
2032 is a Leap Year.

PHP Program To Display Text Messages


<!DOCTYPE html>
<html>
<body>
 
<?php
print "<h1>Mc Donalds</h1>";
print "Burger!<br>";
print "I'm Loving It!<br>";
?>
 
</body>
</html>
Output
Mc Donalds
Burger!
I'm Loving It!

PHP Program To Print An Array


<!DOCTYPE html>
<html>
<body>
 
<?php
$food = array("PIZZA", "BURGER", "COKE");
echo "I love to eat " . $food[0] . ", " . $food[1] . " and " . $food[2] . ".";
?>
 
</body>
</html>
Output
I love to eat PIZZA, BURGER and COKE.

PHP Program To Count Number Of Elements In An Array


<!DOCTYPE html>
<html>
<body>
 
<?php
$food = array("PIZZA", "BURGER", "COKE");
echo count($food);
?>
 
</body>
</html>
Output
3

PHP Program To Print Each Element Of An Array


<!DOCTYPE html>
<html>
<body>
 
<?php
$cars = array("BMW", "Ford", "Hyundai", "Jaguar");
 
foreach ($cars as $value) {
echo "$value <br>";
}
?>
 
</body>
</html>
Output
BMW
Ford
Hyundai
Jaguar

PHP Program To Sort Elements Of An Array In Ascending Order


<!DOCTYPE html>
<html>
<body>
 
<?php
$numbers=array("100","160","20","67");
sort($numbers);
foreach( $numbers as $n )
{
echo "$n<br />";
}
?>
 
</body>
</html>
Output
20
67
100
160

PHP Program To Sort Elements Of An Array In Descending Order


<!DOCTYPE html>
<html>
<body>
 
<?php
$numbers=array("100","160","20","67");
rsort($numbers);
foreach( $numbers as $n )
{
echo "$n<br />";
}
?>
 
</body>
</html>
Output
160
100
67
20

PHP Program To Find The Sum Of Elements In An Array


<!DOCTYPE html>
<html>
<body>
 
<?php
$numbers=array("100","160","20","67");
echo "Sum of array elements is: ";
echo array_sum($numbers);
?>
 
</body>
</html>
Output
Sum of array elements is: 347

PHP Program To Find The Product Of Elements In An Array


<!DOCTYPE html>
<html>
<body>
 
<?php
$numbers=array("10","20","30","40");
echo "Product of array elements is: ";
echo array_product($numbers);
?>
 
</body>
</html>
Output
Product of array elements is: 240000

PHP Program To Split A String As Array Elements Based On Delimiter


<!DOCTYPE html>
<html>
<body>
 
<?php
$str = "Have Courage. Live Free";
print_r (explode(" ",$str));
?>
 
</body>
</html>
Output
Array ( [0] => Have [1] => Courage. [2] => Live [3] => Free )

PHP Program To Combine The Array Elements Into A String With Given Delimiter
<!DOCTYPE html>
<html>
<body>
 
<?php
$arr = array("Have", "Courage.", "Live", "Free.");
print_r (implode(" ",$arr));
?>
 
</body>
</html>
Output
Have Courage. Live Free.

PHP Program To Join The Array Elements Into A String


<!DOCTYPE html>
<html>
<body>
 
<?php
$arr = array("Have", "Courage.", "Live", "Free.");
echo join(" ",$arr);
?>
 
</body>
</html>
Output
Have Courage. Live Free.

PHP Program To Merge Two Arrays Into A New Array


<!DOCTYPE html>
<html>
<body>
 
<?php
$arr1 = array("London","Paris");
$arr2 = array("Switzerland","Scotland");
print_r(array_merge($arr1,$arr2));
?>
 
</body>
</html>
Output
Array ( [0] => London [1] => Paris [2] => Switzerland [3] => Scotland )

PHP Program To Remove The Duplicate Values From An Array


<!DOCTYPE html>
<html>
<body>
 
<?php
$arr = array("London","Paris","Switzerland","Switzerland","Scotland");
print_r(array_unique($arr));
?>
 
</body>
</html>
Output
Array ( [0] => London [1] => Paris [2] => Switzerland [4] => Scotland )

PHP Program To Create A Simple Calculator


<!DOCTYPE html>
<html>
<body>
 
<?php
ini_set('display_errors',0);
 
if( isset( $_REQUEST['calculate'] ))
{
$operator=$_REQUEST['operator'];
$n1 = $_REQUEST['first_value'];
$n2 = $_REQUEST['second_value'];
 
if($operator=="+")
{
$res= $n1+$n2;
}
if($operator=="-")
{
$res= $n1-$n2;
}
if($operator=="*")
{
$res =$n1*$n2;
}
if($operator=="/")
{
$res= $n1/$n2;
}
 
if($_REQUEST['first_value']==NULL || $_REQUEST['second_value']==NULL)
{
echo "<script language=javascript> alert(\"Please Enter Correct values.\");</script>";
}
}
?>
 
<form>
<table style="border:groove #00FF99">
 
<tr>
<td style="background-color:turquoise; color:black; font-family:'Times New Roman'">Enter Number</td>
<td colspan="1">
<input name="first_value" type="text" style="color:red"/></td>
</tr>
 
<tr>
<td style="color:red; font-family:'Times New Roman'">Select Operator</td>
<td>
<select name="operator" style="width: 63px">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select></td>
</tr>
 
<tr>
<td style="background-color:turquoise; color:black; font-family:'Times New Roman'">Enter Number</td>
<td class="auto-style5">
<input name="second_value" type="text" style="color:red"/></td>
</tr>
 
<tr>
<td></td>
<td><input type="submit" name="calculate" value="Calculate" style="color:wheat;background-color:rosybrown
</tr>
 
<tr>
<td style="background-color:turquoise;color:black">Output = </td>
<td style="color:darkblue"><?php echo $res;?></td>
</tr>
 
</table>
</form>
 
</body>
</html>

PHP Program To Create Login And Logout Using Sessions


// Form.php
<!DOCTYPE html>
<html>
<body>
 
<form method="post" action="login.php">
User Id: <input type="text" name="userid"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
 
</body>
</html>
// Login.PHP
<!DOCTYPE html>
<html>
<body>
 
<?php
$uid = $_POST['userid'];
$pw = $_POST['password'];
 
if($uid == 'ben' and $pw == 'ben23')
{
session_start();
$_SESSION['sid']=session_id();
echo "Logged in successfully";
}
?>
 
</body>
</html>
// Logout.PHP
<!DOCTYPE html>
<html>
<body>
 
<?php
 
echo "Logged out successfully";
 
session_start();
session_destroy();
 
?>
</body>
</html>

You might also like