Edo
Edo

Reputation: 1

calculating a circle

I did this but I can't see the PHP part when I tried to view it on Internet Explorer.

This is the code I did:

<html>
<head><title>Practise</title></head>
<body>

<form method="post">
Circumference of a Circle or the Area: <br>

The Radius of the circle: <input type="text" name="radius"><br>

<input type="submit" value="Submit">    

<?php

$rad = (float) $_POST['radius']; 
$cir = $rad * 2 * pi();
$area = pow($rad, 2) * pi();

echo "The circumference of the circle is:" $cir.; 
echo "The area of the circle is:" $area.;  
?>

</body>

</html>

Please state the wrong code. Thank you!

Upvotes: 0

Views: 3775

Answers (4)

bfontaine
bfontaine

Reputation: 19817

This is better:

...
<form method="post" action="this-page.php">

Circumference of a Circle or the Area: <br />

The Radius of the circle: <input type="text" name="radius" /> <br />

<input type="submit" value="Submit" />

</form>

<?php

if (array_key_exists("radius", $_POST)) {

    $rad = (float) $_POST['radius'];
    $cir = $rad * 2 * pi();
    $area = pow($rad, 2) * pi();

    echo "The circumference of the circle is: $cir.";
    echo "The area of the circle is: $area.";
}
?>
...

Upvotes: 0

F&#233;lix Saparelli
F&#233;lix Saparelli

Reputation: 8719

Well first you got string concatenation wrong:

$result = "String " $var.;  // Wrong
$result = "String " . $var; // Right
$result = "String $var";    // Right too.
$result = "String ", $var;  // Also right.

Then you should really do some input checking:

if (!empty($_POST['radius']) {
  // ...
}

There's also a closing </form> tag missing, as well as the action="..." attribute on the <form> tag -- although this should default to the page itself.

And finally it's 'Practice', not 'Practise'... :)

Upvotes: 1

John Green
John Green

Reputation: 13435

Your echos are broken:

echo 'The circumference of the circle is:'.$cir.'.'; 
echo 'The area of the circle is:'.$area.'.';  

Upvotes: 0

cbrandolino
cbrandolino

Reputation: 5883

The two echo lines should be:

echo "The circumference of the circle is:".$cir; 
echo "The area of the circle is:".$area; 

The concatenation operator (point) goes between the strings you want to merge.

Your current code isn't executed because of a syntax error.

Upvotes: 1

Related Questions