Challenge 2 — Solution
Task: Write a function calculateRectangleArea($width, $height = 1) with
a default height of 1, so it can also be used to calculate the area of
a square just by passing one argument. Call it three different ways
and echo each result.
"; // a real rectangle
echo calculateRectangleArea(4) . "
"; // height defaults to 1
echo calculateRectangleArea(6, 6) . "
"; // a square, given explicitly
?>
Output:
15
4
36
Notes:
- calculateRectangleArea(5, 3) supplies both arguments, so the default
is never used - the area is simply 5 * 3.
- calculateRectangleArea(4) supplies only $width, so $height falls back
to its default value of 1 - the area becomes 4 * 1 = 4, which also
happens to demonstrate that omitting the height doesn't cause an
error the way it would without a default value.
- calculateRectangleArea(6, 6) shows the same function working equally
well for a square, just by passing equal width and height explicitly
rather than relying on the default.