php
  1. php-area-of-triangle

PHP Area of Triangle

Syntax

function areaOfTriangle($base, $height) {
  $area = 0.5 * $base * $height;
  return $area;
}

Example

$base = 10;
$height = 5;
$area = areaOfTriangle($base, $height);
echo "The area of the triangle with base {$base} and height {$height} is {$area}.";

Output

The area of the triangle with base 10 and height 5 is 25.

Explanation

The areaOfTriangle() function accepts two parameters: $base and $height. It first calculates the area of the triangle using the formula 0.5 * base * height and stores it in a variable $area. The function then returns this value.

In the example, we assign the values 10 and 5 to $base and $height respectively. We then call the areaOfTriangle() function with these values and store the result in a variable $area. Finally, we print the result using echo.

Use

You can use this function to calculate the area of a triangle with known base and height values.

Important Points

  • The base and height values should be in the same unit of measurement.
  • The result will be in square units of the measurement used for base and height.
  • The formula for the area of a triangle is (1/2) * base * height.

Summary

  • The areaOfTriangle() function calculates the area of a triangle using its base and height values.
  • The function returns the calculated area value.
  • The formula for the area of a triangle is (1/2) * base * height.
Published on: