Member Avatar for LastMitch

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!

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;

Change line 3 to:

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

Oops, my bad. Thx.

Member Avatar for LastMitch

@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.

@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.

Member Avatar for LastMitch

@pritaeas

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

Thanks for the reply and the changes!

Member Avatar for LastMitch

@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!

Member Avatar for LastMitch

@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!

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;
Member Avatar for LastMitch

@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

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.

commented: Thanks for the solution! +4
Member Avatar for LastMitch

@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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.