Click to See Complete Forum and Search --> : PhP scripting help


benzspida
03-30-2006, 07:36 PM
can any one help me with this script I am just gettin started with PHP

I am trying to create an calculation script that would
calculate the

Constant: PI = 3.1415926535
Radius, area, and circumference of a circle .


this is what i have done with the script so far I am getting results, which is good but not

getting the calculation of the area and circumference of the circle

<html>
<head>
<title>
Calculation Form
</title>
</head>
<body>

<?php
if (!isset($_POST['submit'])) {
$result = '<form action="" . method="post">' .
'<input type="text" name="radius" />' .
'<input type="submit" name="submit" value="Calculate" />' .
'</form>';
} else {
define('M_PI', '3.141....');
$circumference = M_PI * $_POST['radius'] * 2;
$area = M_PI * pow($_POST['radius'], 2);
$result .= 'Circumference = ' . $circumference . ' units<br />';
$result .= 'Area = ' . $area . ' sq. units<br />';
print " $result";
}

?>
<html>
<head>
<title></title>
</head>

<body>
<p><? echo "$result" ; ?></p>
</body>
</html>

Do i have to define M_PI? to get the calculations.
thanks for the help

rch10007
03-30-2006, 08:01 PM
Works for me, if the formulas are right - didn't check them:

<html>
<head>
<title>
Calculation Form
</title>
</head>
<body>

<?php
if (!isset($_POST['submit'])) {

print '<form action="'.$_SERVER['PHP_SELF'].'" . method="POST">'."\n".
'<input type="text" name="radius" />'."\n".
'<input type="submit" name="submit" value="Calculate" />'."\n".
'</form>'."\n";

}else{

define('M_PI', '3.1415926535');

$circumference = M_PI * $_POST['radius'] * 2;
$area = M_PI * pow($_POST['radius'], 2);

$result .= '<p>Circumference = ' . $circumference . ' units</p>'."\n";
$result .= '<p>Area = ' . $area . ' sq. units</p>'."\n";

print $result;
}
?>

</body>
</html>

benzspida
03-30-2006, 09:12 PM
not working for me dont know why

balloonbuffoon
03-30-2006, 09:26 PM
It works for me also, but anyway, why are you redefining pi? And what exactly are your getting as output; something like this?

Circumference = units

Area = sq. units

Or nothing at all?

felgall
03-30-2006, 09:56 PM
You can't redefine M_PI as PHP has it defined as a constant and you can't redefine constants (besides which it already has it defined to the maximum number of decimal places that PHP can handle so even if you could redefine a constant you would either be replacing it with a less accurate value for PI or would be adding extra decimal places that will be immediately discarded).

benzspida
03-30-2006, 10:31 PM
balloonbuffoon i get what u have

just not getting any number

this is what i keep gettin
Circumference = 0units

Area = 0 sq. units

balloonbuffoon
03-31-2006, 09:21 PM
If you still haven't resolved this, try taking out the line where you define PI. I dunno what else to try, as it seems to be working for everyone who's tried it, except you, which is kind of strange.

--Steve

rch10007
03-31-2006, 10:17 PM
This may be a stupid question, but are you putting any values into the fields before you submit - the page works for me. I made some changes above to the script, did you try it?