Skip to main content

Simple PHP programs

 1. Print Hello World using php

<?php

    echo "hello world";

?>

 

2. Addition of 5 numbers

<?php

    $= 5;

    $= 6;

    $= 5;

    $= 2;

    $= 9;

 

    echo $+ $+ $+ $+ $e;

?>

a. 
3. Add, multi, sub, div all operators in one program

<?php

    $= 5;

    $= 10;

 

    echo "addition is ", $+ $b,"<br>";

    echo "multiplication is ", $* $b,"<br>";

    echo "division is ", $/ $b, "<br>";

    echo "substraction is ", $- $,"<br>";

 

?>

 

4. Area of circle

<?php

    $= 5;

    $pi = 3.14;

    $= $pi *($r*$r);

    echo "area of circle is ", $a;

?>

 

5. Area of triangle

<?php

    $base = 10;

    $height = 15;

 

    echo "Area of triangle ",($base * $height)/2 ;

?>

 

6. Area of rectangle

<?php

    $width = 20;

    $length = 10;

 

    echo "area of rectangle is ". $width * $length;

?>

 

7. Except the marks of all subject and find out percentage and grade 

<?php

    $sub1 = 70;

    $sub2 = 80;

    $sub3 = 90;

    $sub4 = 95;

    $sub5 = 85;

    $total = $sub1 + $sub2 + $sub3 + $sub4 + $sub5;

    echo "percentage is ", $total / 500 * 100;

?>

 

8. Even & odd

<?php

    $= 2;

 

    if($% 2 == 0){

        echo "no is odd";

    }else{

        echo "no is even";

    }

?>

 

9. Positive negative

<?php

    $= -5;

    if($< 0){

        echo "no is negative";

    }else{

        echo "no is positive";

    }

?>

 

10. Sum of digit

<?php  

$num = 14597;  

$sum=0; $rem=0;  

  for ($i =0; $i<=strlen($num);$i++)  {  

  $rem=$num%10;  

  $sum = $sum + $rem;  

  $num=$num/10;  

  }  

 echo "Sum of digits 14597 is $sum";  

?>  

 

11. prime number

<?php  

$count = 0;  

$num = 2;  

while ($count < 15 )  

{  

$div_count=0;  

for ( $i=1; $i<=$num; $i++)  

{  

if (($num%$i)==0)  

{  

$div_count++;  

}  

}  

if ($div_count<3)  

{  

echo $num." , ";  

$count=$count+1;  

}  

$num=$num+1;  

}  

?>  

 

12. perfect number

<?php

// Function to check if a number is perfect 

function isPerfectNumber($N)

{

    // To store the sum 

    $sum = 0;

       

    // Traversing through each number 

    // In the range [1,N

    for ($i = 1; $i < $N; $i++)

    {

        if ($% $i == 0)

        {

            $sum = $sum + $i;

        }       

    }

      

    // returns True is sum is equal 

    // to the original number. 

    return $sum == $N;

}

   

// Driver's code 

$= 6;

  

if (isPerfectNumber($N))

    echo " Perfect Number";

else

    echo "Not  Perfect Number";

?>

13. Print the table of 27

php

define('a', 7);   

for($i=1; $i<=10; $i++)   

<?{   

  echo $i*a;   

  echo '<br>';     

}  

?>  

 

14. Factorial number

<?php  

$num = 4;  

$factorial = 1;  

for ($x=$num; $x>=1; $x--)   

{  

  $factorial = $factorial * $x;  

}  

echo "Factorial of $num is $factorial";  

?>  

 

15. Amstrong number

<?php  

$num=407;  

$total=0;  

$x=$num;  

while($x!=0)  

{  

$rem=$x%10;  

$total=$total+$rem*$rem*$rem;  

$x=$x/10;  

}  

if($num==$total)  

{  

echo "Yes it is an Armstrong number";  

}  

else  

{  

echo "No it is not an armstrong number";  

}  

?>  

 

16. Palindrome number

<?php  

function palindrome($n){  

$number = $n;  

$sum = 0;  

while(floor($number)) {  

$rem = $number % 10;  

$sum = $sum * 10 + $rem;  

$number = $number/10;  

}  

return $sum;  

}  

$input = 1235321;  

$num = palindrome($input);  

if($input==$num){  

echo "$input is a Palindrome number";  

} else {  

echo "$input is not a Palindrome";  

}  

?>  

 

17. Reverse number

<?php  

$num = 23456;  

$revnum = 0;  

while ($num > 1)  

{  

$rem = $num % 10;  

$revnum = ($revnum * 10) + $rem;  

$num = ($num / 10);   

}  

echo "Reverse number of 23456 is: $revnum";  

?>  

 

18. String reverse

<?php  

$string = "JAVATPOINT";  

echo "Reverse string of $string is " .strrev ( $string );  

?>  

 

19. Swiping of number

<?php  

$= 45;  

$= 78;  

// Swapping Logic  

$third = $a;  

$= $b;  

$= $third;  

echo "After swapping:<br><br>";  

echo "a =".$a."  b=".$b;  

?>  

 

20. Leap year

<?php  

function isLeap($year)  

{  

    return (date('L', mktime(0, 0, 0, 1, 1, $year))==1);  

}  

//For testing  

for($year=1991; $year<2016; $year++)  

{  

    If (isLeap($year))  

    {  

        echo "$year : LEAP YEAR<br />\n";  

    }  

    else  

    {  

        echo "$year : Not leap year<br />\n";  

    }  

}  

?>  

 

21. Star pattern

<?php  

for($i=0;$i<=5;$i++){  

for($j=1;$j<=$i;$j++){  

echo "";  

}  

echo "<br>";  

}  

?>  

 

22. PHP program to print each element of an array

<?php

// PHP program to print all 

// the values of an array 

    

// given array 

$array = array("Geek1", "Geek2",

        "Geek3", "1", "2","3");

 

// Loop through array 

foreach($array as $item){

    echo $item . "\n";

}

 

?>

 

23. PHP program to find number of elements in an array 

php  

    $ele = array("Ryan", "Ahana", "Ritvik", "Amaya");  

    $no_of_ele = count($ele);  

    echo "Number of elements present in the array: ".$no_of_ele;  

?>  

 

 

24. PHP program to sort element in an array in ascending order

<?php

$cars = array("Volvo", "BMW", "Toyota");

sort($cars);

?>

 

 

25. PHP program to sort element in an array in descending order

<?php

$cars = array("Volvo", "BMW", "Toyota");

rsort($cars);

?>

 

26. PHP program to find the sum of element in an array

<?php

$a=array(5,15,25);

echo array_sum($a);

?>

 

 

27. Find maximum number

<?php

echo(max(44,16,81,12));

?>

 

28. Find minimum number

<?php

echo(min(22,14,68,18,15) . "<br>");

?>

Comments