The table I am trying to make should:
The first table should include the results of using PHP to calculate the area and circumference of a circle
Additionally, I am trying to make the answers rounded to the first 2 decimals in the floating point number. Any help would be appreciated.
<!DOCTYPE html>
<!--
Author:Randy Gilman
-->
<?php
$cir_area = M_PI * sqrt(2.65)
$cir_circum = 2 * M_PI * 2.65
?>
<html>
<head>
<meta charset="UTF-8">
<title> Randy's Table</title>
</head>
<body>
<table border = "5px">
<tr>
<th> Shape </th>
<th> Parameter and Values </th>
<th> Area </th>
<th> Perimeter </th>
</tr>
<tr>
<th> Circle </th>
<th> radius = 2.65 meters </th>
<th> <?php echo "$cir_area"?> </th>
<th> <?php echo "$cir_circum"?> </th>
</tr>
</table>
</body>
</html>
PHP uses semicolons to end a line:
echo "Hello";
If you try to run your code, you'll get this error:
Message : syntax error, unexpected '$cir_circum' (T_VARIABLE)
This means, you forgot to end the lines with a semicolon.
Your PHP should look like this:
<?php
$cir_area = M_PI * sqrt(2.65);
$cir_circum = 2 * M_PI * 2.65;
?>
To round numbers, you could use number_format
:
<?php
$cir_area = number_format(M_PI * sqrt(2.65), 2);
$cir_circum = number_format(2 * M_PI * 2.65, 2);
?>
<th> <?php echo number_format($cir_area,2);?> </th>
<th> <?php echo number_format($cir_circum,2);?> </th>
sqrt()
(short for "square root") is a function that computes the square root of its argument.
The formula of circle area doesn't use the square root but the square of its radius (i.e. R*R
).
The calculation should be:
$radius = 2.65; // put it into a variable for clear and easy to change code
$cir_area = M_PI * $radius * $radius;
$cir_circum = 2 * M_PI * $radius;
Remark: PHP statements ends with semicolon (;
). You forgot to put it in your code and it that's why it doesn't compile and displays errors (or nothing at all) instead of the expected HTML output.
You can use the function number_format()
to display the floating point numbers using a specified number of decimal digits.
The code:
<tr>
<td>Circle</td>
<td>radius = <?php echo(number_format($radius, 2)); ?> meters</td>
<td><?php echo(number_format($cir_area, 2)); ?> square meters</td>
<td><?php echo(number_format($cir_circum, 2)); ?> meters</td>
</tr>
please use the semicolon after every statement in PHP $cir_area = M_PI * sqrt(2.65);
, and for the rounding to the first 2 decimals in the floating point number you can use string number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," )
and in your code i made some changes and its works
$cir_area = number_format(M_PI * sqrt(2.65), 2, '.', '');
$cir_circum = number_format(2 * M_PI * 2.65, 2, '.', '');
<tr>
<th> <?php echo $cir_area?> </th>
<th> <?php echo $cir_circum?> </th>
</tr>