2

Hi

I'm trying to figure out how to <input> $radius and echo $pi without using a DB.

This is a simple example how this calculate $pi which you can run and see how it works:

<?php
$pi = "3.1415927";
$radius = 8;
echo $pi * ($radius * $radius);
?>

The past few days I been trying to figure out how to <input> $radius and then calculate $pi in a different page:

This is my form:

<html>
<body>

<form action="radius.php" method="get">
$radius: <input type="text" name="radius" /><br />
<input type="submit" value="Submit">
</form>

</body>
</html>

This is my radius.php, this where it does the calculation:

<?php
$pi = "3.1415927";
$radius = ' ';
?>

This is my output:

<?php echo $pi * ($radius * $radius); ?>

I appreciated any suggestions or comments and also let me know what I did wrong or I might overlook something.

Thanks!

3
Contributors
12
Replies
14
Views
4 Years
Discussion Span
Last Post by LastMitch
Featured Replies
  • **@blocblue** Thanks for the reply and example. I test it out and it came back: **0** When I put **6** in <input> $radius it came out: 0 From my old example which is this I put 6: <?php $pi = "3.1415927"; $radius = 6; echo $pi * ($radius * $radius); … Read More

  • **@pritaeas** >$radius = isset($_GET['radius']) ? (float)$_GET['radius'] : 0; Thanks for the reply and the changes! Read More

  • **@blocblue** >Change line 5 to: >$radius = isset($_GET['radius']) ? (float)$_GET['radius'] : 0; >Also note that M_PI; is a PHP constant for Pi I will make some adjustments! Read More

  • **@pritaeas @blocblue** The changes didn't work correctly. From my old example which is this I put **12**: <?php $pi = "3.1415927"; $radius = 12; echo $pi * ($radius * $radius); ?> The answer is this: `452.3893488` Ok this is what I change so far: <html> <body> <form action="radius.php" method="get"> radius: … Read More

  • **@blocblue** I made some adjustments and did what you did and the answer is very close this time: `452.38934211693` = `452.3893488` How do I round this `($radius * $radius)`? I put ( ) on the `$radius * $radius` echo ($radius * $radius) * M_PI; I still get `452.38934211693` Read More

0

So you have your form with an element called radius.

In your radius.php file, just retrieve the value from the URL.

<?php

$radius = isset($_GET['radius']) ? (float)$radius : 0;
echo $radius * M_PI;
1

Change line 3 to:

$radius = isset($_GET['radius']) ? (float)$_GET['radius'] : 0;
Votes + Comments
Thanks for the correction!
2

@blocblue

Thanks for the reply and example.

I test it out and it came back: 0

When I put 6 in <input> $radius it came out: 0

From my old example which is this I put 6:

<?php
$pi = "3.1415927";
$radius = 6;
echo $pi * ($radius * $radius);
?>

The answer is this: 113.0973372

Ok this is what I change so far:

<html>
<body>
<form action="radius.php" method="get">
 radius: <input type="text" name="radius" /><br />
<input type="submit" value="Submit">
</form>
</body>
</html>

-

<?php
$pi = "3.1415927";
$radius = ' ';

$radius = isset($_GET['radius']) ? (float)$radius : 0;
echo $radius * $pi;
?>

I apprecaite your help.

0

@pritaes spotted a mistake in my orignal post.

Change line 5 to:
$radius = isset($_GET['radius']) ? (float)$_GET['radius'] : 0;

Also note that M_PI; is a PHP constant for Pi, to save you declaring it if you wish.

2

@pritaeas

$radius = isset($_GET['radius']) ? (float)$_GET['radius'] : 0;

Thanks for the reply and the changes!

Edited by LastMitch: grammer

2

@blocblue

Change line 5 to:
$radius = isset($_GET['radius']) ? (float)$_GET['radius'] : 0;

Also note that M_PI; is a PHP constant for Pi

I will make some adjustments!

2

@pritaeas
@blocblue

The changes didn't work correctly.

From my old example which is this I put 12:

<?php
$pi = "3.1415927";
$radius = 12;
echo $pi * ($radius * $radius);
?>

The answer is this: 452.3893488

Ok this is what I change so far:

<html>
<body>
<form action="radius.php" method="get">
radius: <input type="text" name="radius" /><br />
<input type="submit" value="Submit">
</form>
</body>
</html>

-

<?php
$pi = "3.1415927";
$radius = ' ';
$radius = isset($_GET['radius']) ? (float)$_GET['radius'] : 0;
echo $radius * M_PI;
?>

I enter 12 the answer is came out: 37.699111843078

$pi * $radius = 37.699111843078

The $pi is correct! =)

But shouldn't $pi = $pi ?

My equation is

$pi * ($radius * $radius)

Meaning

$pi = "3.1415927" * ($radius = 12 * $radius = 12) = 452.3893488

How do I get $radius = 12 * $radius = 12?

I need that extra $radius to balance out the equation.

452.3893488 = 37.699111843078

I made my owns changes but an error came out:

syntax error, unexpected ':'

The little change I made is

<?php
$pi = "3.1415927";
$radius = ' ';
$radius = isset($_GET['radius']) * isset($_GET['radius']) : 0;
echo $radius * M_PI;
?>

Any Suggestions? Thanks!

0

The first line was simply to find the radius value entered in the form. Leave that as it is.

When you have that value, you'd need to square it (multiply it by itself) and then by Pi to get the area of a circle with the specified radius.

<?php

// Find radius
$radius = isset($_GET['radius']) ? (float)$_GET['radius'] : 0;

// Output area of circle with specified radius
echo $radius * $radius * M_PI;
2

@blocblue

I made some adjustments and did what you did and the answer is very close this time:

452.38934211693 = 452.3893488

How do I round this ($radius * $radius)?

I put ( ) on the $radius * $radius

echo ($radius * $radius) * M_PI;

I still get 452.38934211693

1

You can round the value using the round() function. The second parameter is the number of decimal places you'd like the value rounded to.

<?php

// Find radius
$radius = isset($_GET['radius']) ? (float)$_GET['radius'] : 0;

// Calculate area of circle with specified radius
$area = round($radius * $radius * M_PI, 7);
echo $area;

If you're still not getting the answer you want, you might want to replace the M_PI with your own value, as the constant is likely more precise to more decimal places than your chosen value.

Edited by blocblue

Votes + Comments
Thanks for the solution!
2

@blocblue

I made some adjustment and it work!

This is the whole equation:

From my old example which is this I put 25:

<?php
$pi = "3.1415927";
$radius = 25;
echo $pi * ($radius * $radius);
?>

The answer is this: 1963.4954375

Here is my form:

<html>
<body>
<form action="radius.php" method="get">
radius: <input type="text" name="radius" /><br />
<input type="submit" value="Submit">
</form>
</body>
</html>

-

<?php
$pi = "3.1415927";
$radius = ' ';

$radius = isset($_GET['radius']) ? (float)$_GET['radius'] : 0;
$area = round($radius * $radius * $pi, 7);
echo $area;
?>

I enter 25 the answer is came out: 1963.4954375

The equation balance out!

$pi * ($radius * $radius) = $pi * ($radius * $radius)

which is

1963.4954375 = 1963.4954375

Thanks Man! I appreciate your help!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.