PHP Functions

Factorial program in PHP using recursive function

Exercise Description:
Write a PHP program to find factorial of a number using recursive function.

What is Recursive Function?

  • A recursive function is a function that calls itself.

Factorial program in PHP using recursive function

<?php
function factorial($number) { 
 
    if ($number < 2) { 
        return 1; 
    } else { 
        return ($number * factorial($number-1)); 
    } 
}
 
echo factorial(4);
?>
Tutorials Class - Output Window

24

Write a PHP program to check if a person is eligible to vote

Description:

Write a PHP program to check if a person is eligible to vote or not.

Condition
Click to View Solution/Program.
<?php
function check_vote() //function has been declared
{
    $name = "Rakesh";
    $age = 19;
    if ($age >= 18) {
        echo $name . ", you Are Eligible For Vote";
    } else {
        echo $name . ", you are not eligible for vote. ";
    }
}
check_vote(); //function has been called
 
?>
Tutorials Class - Output Window

You Are Eligible For Vote


Write a PHP program to calculate area of rectangle

Description:

Write a PHP program to calculate area of rectangle by using PHP Function.

Condition

  • You must use a PHP Function.
  • There should be two arguments i.e. length & width.
View Solution/Program.
<?php
function rect_area($length = 2, $width = 4) //function has declared
{
    $area = $length * $width;
    echo "Area Of Rectangle with length " . $length . " & width " . $width . " is " . $area ;
}
rect_area(); // function has been called.
 
?>
Tutorials Class - Output Window

Area Of Rectangle with length 2 & width 4 is 8 .