Basic PHP Programs

Add two numbers Program in PHP

<?php

$a=10;
$b=20;
$c=$a+$b;

echo "Sum: ",$c;

?>

Swap Two number Program in PHP

Swap two number with each other means interchange their value. Suppose you have two number a and b a contain 10 and b contain value 20 after swap their value, a have 20 and b have 10.

<?php

$a=10;
$b=20;

echo "Value of a: $a</br>";
echo "Value of b: $b</br>";

$temp=$a;
$a=$b;
$b=$temp;

echo "Value of a: $a</br>";
echo "Value of b: $b</br>";

?>

Swap two number without using third variable program in PHP

<?php

$a=10;
$b=20;

echo "Value of a: $a</br>";
echo "Value of b: $b</br>";

$a=$a+$b;
$b=$a-$b;
$a=$a-$b;

echo "Value of a: $a</br>";
echo "Value of b: $b</br>";

?>

Even Odd number Program in PHP

Ever number are those which is divisible by 2 and odd numbers are not divisible by 2. For Example 10 is divisible by 2 so it is even number.

<?php

$num=10;

if($num%2==0)
{
 echo "Even number"; 
}
else
{
 echo "Odd number";
} 

?>

Factorial of a number in PHP

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. But here we write program; how to print factorial of any number in php.

Example of Factorial :

5!= 5 4 3 2 1 = 120

<?php

$num = 4;
$factorial = 1;
for ($x=$num; $x>=1; $x--) 
{
  $factorial = $factorial * $x;
}
echo "Factorial of $num is $factorial";

?>

Armstrong number Program in PHP

A Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits is equal to the number itself..

<?php

$num=153;
$sum=0;
$temp=$num;
while($temp!=0)
{
$rem=$temp%10;
$sum=$sum+$rem*$rem*$rem;
$temp=$temp/10;
}
if($num==$sum)
{
echo "Armstrong number";
}
else
{
echo "not an armstrong number";
}

?>

Palindrome number Program in PHP

Palindrome number means number remain same after reverse any number. For example we reverse 121 result will be 121 so it is palindrome number.

  • Steps to check number is palindrome or not
  • Take a number.
  • Reverse the input number.
  • Compare both the numbers.
<?php

$num = 121;
$p=$num;
$revnum = 0;
while ($num != 0)
{
$revnum = $revnum * 10 + $num % 10;
//below cast is essential to round remainder towards zero
$num = (int)($num / 10); 
}

if($revnum==$p)
{
echo $p," is Palindrome number";
}
else
{
echo $p." is not Palindrome number";
}

?>

Prime number Program in PHP

Here we can write php program to find number is prime or not using for loop. Here also same concept apply like C programming

<?php
$num =23;

    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>';
}

?>

Fibonacci Series Program in PHP

The simple concept to find fibonacci series is; add two previous term and get next term. Example of Fibonacci Series is; 0 1 1 2 3 5.

<?php

function printFibonacci($n)
 {

  $first = 0;
  $second = 1;

  echo "Fibonacci Series \n";

  echo $first.' '.$second.' ';

  for($i = 2; $i < $n; $i++){

    $third = $first + $second;

    echo $third.' ';

    $first = $second;
    $second = $third;

    }
}

/* Function call to print Fibonacci series upto 6 numbers. */

printFibonacci(6);

?>

Reverse number Program in PHP

strrev() function is used to reverse any given number in php, but here we perform this operation without using strrev() function and using strrev() function.

Reverse number without using library function Program in PHP

<?php

$num = 2039;
$revnum = 0;
while ($num != 0)
{
$revnum = $revnum * 10 + $num % 10;
//below cast is essential to round remainder towards zero
$num = (int)($num / 10); 
}

echo "Reverse number: $revnum";

?>

Reverse number Program in PHP

<?php

function reverse_number($number)
{

   /* Typecast the number into string. */

    $snum = (string) $number;

    /* Reverse the string. */

    $revstr = strrev($snum);

    /* Typecast string into int. */

    $reverse = (int) $revstr;

     return $reverse;
}

 echo reverse_number(2039);

?>

Reverse String Program in PHP

strrev() function is used to reverse any given string in php, but here we perform this operation without using strrev() function.

Reverse String Program in PHP

<?php

$string = "hitesh";
$length = strlen($string);

for ($i=($length-1) ; $i >= 0 ; $i--) 
{
  echo $string[$i];
}
?>