/*
Calculate the volume of a cube in PHP.
-----------------------------------------------
A cube has equal side lengths. If each side has length "side",
the volume is:
volume = side ** 3
PHP supports exponentiation using the ** operator.
*/
/**
* Compute the volume of a cube.
*
* @param float $side Length of one side of the cube (must be non‑negative).
* @return float The cube's volume.
*/
function cubeVolume(float $side): float {
if ($side < 0) {
throw new InvalidArgumentException("Side length must be non‑negative");
}
// PHP's exponent operator is idiomatic and efficient.
return $side ** 3;
}
// Main
$side = 6.0;
echo "Side length: $side\n";
echo "Volume of cube: " . cubeVolume($side) . "\n";
/*
run:
Side length: 6
Volume of cube: 216
*/