Find the Perimeter of a Rectangle in PHP



Problem Description

Given the length and breadth of a rectangle, we need to implement a PHP program to find the perimeter. A rectangle is a closed two-dimensional figure having 4 sides. The opposite sides are equal and parallel. The angle made by the adjacent side is equal to 90 degrees. The perimeter is the sum of all sides; in other words, the perimeter is two times the sum of length and breadth. In this tutorial, we are going to learn how we can find the perimeter of a given rectangle in PHP using different approaches.

Formula of Perimeter of Rectangle

The formula of the perimeter of a rectangle is as follows ?

$${Perimeter \: of \: Rectangle = 2 × (length + breadth)}$$

Where,

  • length ? is the horizontal distance.
  • breadth ? is the vertical distance.

Examples

Let's understand the problem description with the help of some input-output examples.

Input

length = 10 unit
breadth = 5 unit

Output

30 unit

Explanation

Using the formula to calculate perimeter: 2 × (Length + Breadth).

perimeter = 2 × (10 + 5)
= 2 × (15)
= 30 unit

Input

length = 20 unit
breadth = 0 unit

Output

0

Explanation

If the length or breadth of any dimension is zero then a 2-dimensional figure does not exist. So, there is no perimeter of a non-existing rectangle figure.

Input

length = 15 unit
breadth = 10 unit

Output

50 unit

Explanation

Using the formula to calculate perimeter: 2 × (length + breadth)

2 × (15 + 10)
2 × (25)
50 unit

Approach 1: Direct Formula

We use the direct formula approach to calculate the perimeter of the rectangle using PHP. The formula for calculating the perimeter of the rectangle is: length + breadth + length + breadth

The formula for the perimeter of the rectangle = 2 × (Length + Breadth). After calculating the perimeter return the result.

Steps for Implementation

Follow the below steps ?

  • Take length and breadth as input parameters.
  • Calculate the perimeter of the rectangle using the formula: 2 × (Length + Breadth)
  • After calculating, print the result.

Example

The below example is the implementation in PHP following the above-discussed implementation steps.

<?php
$length = 10;
$width = 5;

// Calculate perimeter using the formula
$perimeter = 2 * ($length + $width);

// Output
echo "The perimeter of the rectangle with length $length and width $width is: $perimeter
"; ?>

Output

The perimeter of the rectangle with length 10 and width 5 is: 30

Time Complexity: O(1) , constant time
Space Complexity: O(1) , constant space.

Approach2: Using Function

We will use a function to calculate the perimeter of the rectangle. The logic and formula for calculating the perimeter of the rectangle are the same. We will use a function so that we can reuse code later by simply changing the value.

Steps for Implementation

Follow the below steps ?

  • We create a function to calculate the perimeter which calculates the perimeter of a rectangle using the formula ${2 * ($length + $width)}$.
  • We pass the input value length and breadth.
  • Finally, we output the result.

Example

The below example is the implementation in PHP following the above-discussed implementation steps.

<?php
// Function to calculate the perimeter
function calculatePerimeter($length, $width) {
return 2 * ($length + $width);
}

// Call the function
$length = 10;
$width = 5;
$perimeter = calculatePerimeter($length, $width);

echo "The perimeter of the rectangle with length $length and width $width is: $perimeter
"; ?>

Output

The perimeter of the rectangle with length 10 and width 5 is: 30

Time Complexity: O(1) , single function call.
Space Complexity:: O(1), as the above function, also uses only a few variables.

Updated on: 2024-12-10T14:48:23+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements