PHP Basic Programs
PHP Basic Programs
PHP Basic Programs
These are some of the Basic Programs written in PHP
1.Sum of elements of an array
2.Average of elements of array
3.Largest element in array
4.Smallest element in array
5.To convert digit to word
6.Function to swap two numbers using call by reference
7.Function to return sum of digits
8.Function to print arrays in reverse order
9.Function to return reverse of digits entered by parameter
10.Function to check number is even or odd
11.Function to check number is prime or not
12.Function to find power of a number entered by parameter.
13.Function to check whether the given number is palindrome or not
14.Function to convert decimal number to binary number
10.Function to check number is even or odd
11.Function to check number is prime or not
12.Function to find power of a number entered by parameter.
13.Function to check whether the given number is palindrome or not
14.Function to convert decimal number to binary number
1. Program to find sum of elements of an array
<?php
$a = array (1,2,3,4,5);
$sum=0;
$i=0;
for($i=0;$i<(count($a));$i++)
{
$sum=$sum + $a[$i];
}
echo ("Sum of elements is:" . $sum);
?>
Output:
Sum
of elements is:15
2. Program to find average of elements of array
<?php
$a = array (1,2,3,4,5);
$sum=0;
$i=0;
for($i=0;$i<(count($a));$i++)
{
$sum=$sum + $a[$i];
}
$average = $sum / count($a);
echo ("Average of elements is:" . $average);
?>
Output:
Average of elements is:3
3.Program to find largest element in array
<?php
$a =array (1,2,3,4,5);
$i=0;
$max=$a[0];
for($i=0;$i<(count($a));$i++)
{
if($a[$i]>$max)
$max = $a[$i];
}
echo ("Maximum of elements in array is:" . $max);
?>
Output:
Maximum of elements in array is:5
4. Program to find smallest element in array
<?php
$a =array (1,2,3,4,5);
$i=0;
$min=$a[0];
for($i=0;$i<(count($a));$i++)
{
if($a[$i]<$min)
$max = $a[$i];
}
echo ("Minimum of elements in array is:" . $min);
?>
Output:
Minimum of elements in array is:1
5. Program to convert digit to word
<?php
$a =array (0,3,5,6,8,1);
$min=$a[0];
for($i=0;$i<(count($a));$i++)
{
switch($a[$i]){
case 0 : echo "Zero" . " ";
break;
case 1 : echo "One". " ";
break;
case 2: echo "Two". " ";
break;
case 3 : echo "Three". " ";
break;
case 4 : echo "Four". " ";
break;
case 5 : echo "Five". " ";
break;
case 6 : echo "Six". " ";
break;
case 7 : echo "Seven". " ";
break;
case 8 : echo "Eight". " ";
break;
case 9 : echo "Nine". " ";
break;
default:
echo "no not found";
}
}
?>
Output:
Zero Three Five Six Eight One
6. Define a function to swap two numbers using call by reference
<?php
function swap(&$x,&$y)
{
$temp;
$temp=$x;
$x=$y;
$y=$temp;
}
$a=10;
$b=20;
swap($a,$b);
echo"A.$a";
echo"<br>";
echo"B.$b";
?>
Output
A.20
B.10
7. Define a function to return sum of digits.
<?php
function sum($x)
{
$n=0;
while($x!=0){
$b=$x%10;
$n=$n+$b;
$x=$x/10;
}
return $n;
}
$a=255;
$k=sum($a);
echo"$k";
?>
Output:
12
8. Define a function to print arrays in reverse order.
<?php
function printrev(&$x)
{
for($i = count($x);$i >= 0; $i--)
echo $x[$i];
}
$a = array(5,6,7,8,9);
printrev($a);
?>
Output:
98765
9. Define a function to return reverse of digits entered by parameter.
<?php
function revdigi($a)
{
$t=0;
while($a>0)
{
$t =$t*10 + $a%10;
$a=((int) ($a/10));
}
return $t;
}
echo revdigi(123456789);
?>
Output
987654321
10. Define a function to check number is even or odd.
<?php
function test($x)
{
if(($x%2) == 0)
echo "Even";
else
echo "Odd";
}
$a= 5;
test($a);
?>
Output:
Odd
11. Define a function to check number is prime or not.
<?php
function testprime($x)
{
for($i=2;($i*$i)<=($x);$i++)
{
$k=$x % $i;
if($x==2)
echo"The number is prime";
else if($k==0 && $i<$x)
{
echo "The number is not prime";
break;
}
else
echo "The number is prime";
}
}
$a=5;
testprime($a);
?>
Output:
The number is prime
12. Define a function to find power of a number entered by
parameter.
<?php
function findpower($x,$y)
{
$k=1;
for($i=0;$i<$y;$i++)
$k=$k*$x;
return $k;
}
echo findpower(5,3);
?>
Output:
125
13. Define a function to check whether the given number is palindrome or not.
<?php
function palindrome($x)
{
$a=$x;$temp=0;
while($a>0)
{
$k=($a%10);
$temp=($temp*10)+$k;
$a=((int)($a/10));
}
if($temp==$x)
echo"Palindrome";
else
echo "Not a Palindrome";
}
$t=123;
palindrome($t);
?>
Output:
Not a Palindrome
14. Define a function to convert decimal number to binary number.
<?php
function convert($x)
{
$a=0;$temp=0;
while($x>0){
$a=$a*10+$x%2;
$x=((int)($x/2));
}
while($a>0)
{
$k=($a%10);
$temp=($temp*10)+$k;
$a=((int)($a/10));
}
echo $temp;
}
$t=123;
convert($t);
?>
Output:
1111011
Comments
Post a Comment
Feel free to ask if you have any queries and if want any information . I feel happy to interact with you all and help you .