Area and Perimeter of Rectangle using Functions

Calculating the area and perimeter of a rectangle is by defining separate functions for each calculation, namely calculateArea and calculatePerimeter.

Example: Illustration of calculating the area and perimeter of a rectangle in PHP using functions.

PHP




<?php
  
// To calculate area
function calculateArea($length, $width) {
    return $length * $width;
}
  
// To calculate perimeter
function calculatePerimeter($length, $width) {
    return 2 * ($length + $width);
}
  
// Given values
$length = 10;
$width = 5;
  
$area = calculateArea($length, $width);
$perimeter = calculatePerimeter($length, $width);
  
echo "Area of Rectangle: $area\n";
echo "Perimeter of Rectangle: $perimeter";
  
?>


Output

Area of Rectangle: 50
Perimeter of Rectangle: 30

Time Complexity: O(1)

Auxiliary Space: O(1)

PHP Program to Find Area and Perimeter of Rectangle

Calculating the area and perimeter of a rectangle is a fundamental geometric operation. The area of a rectangle is given by the formula Area = length * width, and the perimeter is given by Perimeter = 2 * (length + width). In this article, we will explore how to calculate the area and perimeter of a rectangle in PHP using different approaches.

Table of Content

  • Area and Perimeter of Rectangle using Functions
  • Area and Perimeter of Rectangle using a Class

Similar Reads

Area and Perimeter of Rectangle using Functions

Calculating the area and perimeter of a rectangle is by defining separate functions for each calculation, namely calculateArea and calculatePerimeter....

Area and Perimeter of Rectangle using a Class

...

Contact Us