find the area of a regular polygon in php

To find the area of a regular polygon in PHP, you can use the following function:

main.php
function findAreaOfRegularPolygon($numSides, $sideLength) {
  $apothem = $sideLength / (2 * tan(pi() / $numSides));
  $perimeter = $numSides * $sideLength;
  $area = 0.5 * $apothem * $perimeter;
  return $area;
}
213 chars
7 lines

This function takes two parameters:

  • $numSides: the number of sides in the regular polygon
  • $sideLength: the length of each side of the regular polygon

The function first calculates the apothem of the polygon using the formula apothem = s / (2 * tan(pi / n)), where s is the length of each side, n is the number of sides, and pi is the mathematical constant. It then calculates the perimeter of the polygon using the formula perimeter = n * s. Finally, it calculates the area of the polygon using the formula area = 0.5 * apothem * perimeter and returns it.

You can call this function and pass in the number of sides and the length of each side of the regular polygon to find its area.

related categories

gistlibby LogSnag