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:
Tutorials Class - Output Window
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.
Learn more about the similar topics:
| Tutorials |
|---|
| PHP Functions |
| Exercises & Assignments |
|---|
| Factorial program in PHP using recursive function |
| Write a PHP program to check if a person is eligible to vote |
| Write a PHP program to calculate area of rectangle |
| Interview Questions & Answers |
|---|
| What is PHP Function? |
| What is PHP floor() function? |