PHP Functions
PHP function exercises: print numbers with conditions, calculate the area of a rectangle, voting eligibility and a factorial program using a recursive function.
Factorial program in PHP using recursive function
Description: Write a PHP program to find the factorial of a number using a recursive function.
What is a recursive function? A recursive function is a function that calls itself, until a stopping condition is reached.
PHP program for factorial using recursion
<?php
function factorial($n) {
if ($n <= 1) {
return 1; // stopping condition
}
return $n * factorial($n - 1);
}
echo factorial(4) . "<br>";
echo factorial(5);
?>
Output:
24
120
How it works
factorial(4)returns 4 x factorial(3), then 3 x factorial(2), then 2 x factorial(1).- When $n is 1 or less the function returns 1 and stops. Without this condition the function would call itself for ever.
- The results come back one by one: 1, then 2, then 6, then 24.
Compare it with the loop solution: factorial using a for loop.
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.
Rules and hints:
- The minimum age to vote is 18.
- Use a PHP function.
- Use decision making statements.
PHP program to check voting eligibility
<?php
function checkVote($name, $age) {
if ($age >= 18) {
return $name . ", you are eligible to vote.";
}
return $name . ", you are not eligible to vote.";
}
echo checkVote("Rakesh", 19) . "<br>";
echo checkVote("Anita", 16);
?>
Output:
Rakesh, you are eligible to vote.
Anita, you are not eligible to vote.
How it works
- The function gets the name and the age as parameters.
- If the age is 18 or more it returns the eligible message, otherwise the other message.
- We call it two times with different values.
Write a PHP program to calculate area of rectangle
Description: Write a PHP program to calculate the area of a rectangle using a PHP function.
Rules and hints:
- You must use a function.
- The function should have two arguments: length and width.
PHP program to calculate the area of a rectangle
<?php
function rectArea($length, $width) {
return $length * $width;
}
$length = 2;
$width = 4;
echo "Area of rectangle with length " . $length . " and width " . $width . " is " . rectArea($length, $width);
?>
Output:
Area of rectangle with length 2 and width 4 is 8
How it works
- The function multiplies the length with the width and returns the result.
- We print the text and the returned value together with the dot operator.
Try this too
- Give default values to the parameters, like
function rectArea($length = 2, $width = 4), and call it without arguments. - Write a similar function for the area of a circle using
M_PI.
Write a PHP Program to print 5 to 15 using Conditional statements
Description: Write a PHP program to print the numbers from 5 to 15 using conditional statements, without using any loop.
Rules and hints:
- You can use a function with arguments.
- You can not use a loop (
for,while,foreach). - You can use
ifconditions.
PHP program to print 5 to 15 without a loop
<?php
function printNumbers($current, $end) {
if ($current > $end) {
return; // stop
}
echo $current . "<br>";
printNumbers($current + 1, $end); // call the function again
}
printNumbers(5, 15);
?>
Output:
5
6
7
8
9
10
11
12
13
14
15
How it works
- The function prints the current number, and then calls itself with the next number.
- When the current number becomes bigger than the end number, the if condition stops the function. This is called recursion.
Read about PHP functions and see another example of recursion in factorial using recursion.