Hi
I need PHP functions for calculating the area and volume of a cylinder?
I will be thankfull for your answer.

<form action="functions.php" method="POST">

    Enter the radius = <input type="text" name="radius"><br>
    Enter the height = <input type="text" name="height"><br>
    <input type="submit" name="submit" value="Calculate"><br>
</form>




<?php
//calculates the volume of a cylinder
//Volym = π r2 h
function volumeCylinder($r,$h) 
{
    $pi = 3.141592653589;
    $volume = 2 * pi *radius *(radius + height);
    //return volume;
    echo "The volume is", volume;
}






?>
Member Avatar

diafol

So what's the problem? Your formula looks wrong btw.

$vol = pi * height * radius * radius;

or

$vol = pi * height * pow(radius,2);

Use:

return $volume;

So...

echo "The volume is " . volumeCylinder($r,$h);

Thanks sir
Do you know why it dos't display

function volumeCylinder($r,$h) 
{
    $pi = 3.141592653589;
    $vol = pi * height * pow(radius,2);
    return $vol;
    echo "The volume is" .volumeCylinder($r,$h);

}

?>
Member Avatar

diafol

Nothing displays after return. Oh dear. This is what I was suggesting, in full..

function volumeCylinder($r,$h) 
{
    $pi = 3.141592653589;
    $vol = $pi * $h * pow($r,2);
    return $vol;
}

echo "The volume is " .volumeCylinder($r,$h);

Thanks again
I thing I forget $_Post to pass to functions.php but still wrong!!

<?php
if(isset($_POST["submit"])){
    echo $_POST['radius'];
    echo $_POST['height'];
    function volumeCylinder($r,$h)
    {
    $pi = 3.141592653589;
    $vol = $pi * $h * pow($r,2);
    return $vol;
    echo "The volume is " .volumeCylinder($_POST['$r'],$_POST['$h']);
    }

}

?>
Member Avatar

diafol

<?php
if(isset($_POST["submit"])){
   $r = (float) $_POST['radius'];
   $h = (float) $_POST['height'];
    function volumeCylinder($r,$h)
    {
    $pi = 3.141592653589;
    $vol = $pi * $h * pow($r,2);
    return $vol;
    echo "The volume is " .volumeCylinder($_POST['$r'],$_POST['$h']);
    }
}
?>

You have to pass variables to the function, otherwise it won't be able to use them. I strongly suggest you look at some more tutorials and check out the php.net manual.

Thanks sir for you answer.
I know your code is right but still not dispaly in my local server
Have you any idea why?

<!doctype html>
<html>
<head>
    <title> Functions</title>
</head>
<body>

<?php
if(isset($_POST["submit"])){
   $r = (float) $_POST['radius'];
   $h = (float) $_POST['height'];
    function volumeCylinder($r,$h)
    {
    $pi = 3.141592653589;
    $vol = $pi * $h * pow($r,2);
    return $vol;
    echo "The volume is " .volumeCylinder($_POST['$r'],$_POST['$h']);
    }
}
?>

</body>
</html>
Member Avatar

diafol

Sorry put the echo outside the function

//test values
$_POST['submit'] = true;
$_POST['radius'] = 1.2;
$_POST['height'] = 5;



if(isset($_POST["submit"])){
   $r = (float) $_POST['radius'];
   $h = (float) $_POST['height'];
    function volumeCylinder($r,$h)
    {
    $pi = 3.141592653589;
    $vol = $pi * $h * pow($r,2);
    return $vol;

    }
    echo "The volume is " .volumeCylinder($r,$h);
}

Thanks diafol
It’s working very fine now.
Can you give me some tip how can I solve cylinder Area?
Is it right?
area = 2 π r h -> area= 2 * pi * r2 + 2 + pi * radius * height ?
should I have also two parameter i function areaCylinder($r,$h) ?

Member Avatar

diafol

I'd set up a class for this. Sounds scary if you are new to OOP, but it's a nice way of handling this sort of "real world object", such as a shape.

I'm busy at the mo, but will come back in 30 mins with an example.

Thanks
I got the same result as Volume when I calculate Area?why

//Calculate Area
if(isset($_POST["submit"])){
   $ra = (float) $_POST['radius'];
   $ha = (float) $_POST['height'];

   function areaCylinder($ra,$ha)
   {
   $pi = 3.141592653589;
   $cylinder_area = 2 * $pi *($radius * $height);
   return $cylinder_area;

   }
   echo "The area is : " .volumeCylinder($ra,$ha);

}
?>
Member Avatar

diafol

Where's volumeCylinder? Anyway, here's an example...

abstract class Shape
{
    private $a = 0;
    private $b = 0;
    private $areaFunction = '';
    private $volFunction = '';

    public abstract function getArea();
    public abstract function getVolume();
}

class Cylinder extends Shape
{
    public $areaFunction = '2&pi;rh';
    public $volFunction = '&pi;r<sup>2</sup>h';

    public function __construct($radius, $height)
    {
        $this->a = (float) $radius;
        $this->b = (float) $height; 
    }

    public function getArea($precision=2)
    {
        return number_format(2 * M_PI * $this->a * $this->b, $precision);   
    }

    public function getVolume($precision=2)
    {
        return number_format(M_PI * pow($this->a,2) * $this->b, $precision);    
    }
}

class Sphere extends Shape
{
    public $areaFunction = '4&pi;r<sup>2</sup>';
    public $volFunction = '4/3 &pi;r<sup>3</sup>';

    public function __construct($radius)
    {
        $this->a = (float) $radius; 
    }

    public function getArea($precision=2)
    {
        return number_format(4 * M_PI * pow($this->a,2), $precision);   
    }

    public function getVolume($precision=2)
    {
        return number_format(4/3 * M_PI * pow($this->a,3), $precision); 
    }
}


$rad = 5;
$ht = 6;
$sphereRad = 13.55;


$c = new Cylinder($rad,$ht);
$s = new Sphere($sphereRad);

echo "Area of Cylinder Radius $rad and Height $ht [".$c->areaFunction."] is: " . $c->getArea() . "<br />";
echo "Volume of Cylinder Radius $rad and Height $ht [".$c->volFunction."] is: " . $c->getVolume() . "<br />";
echo "Area of Sphere Radius $sphereRad [".$s->areaFunction."] is: " . $s->getArea() . "<br />";
echo "Volume of Sphere Radius $sphereRad [".$s->volFunction."] is: " . $s->getVolume() . "<br />";

The "abstract class" is just a template shape for all other "subclasses" to follow. It suggests which methods and properties its "children" should have. Notice that we use 'client code' outside the classes themselves in order to call properties and run methods.

Thanks Sir
But you really scary me its not for beginners :)

The is all code which give same result as Volume?I don't know why?

<!doctype html>
<html>
<head>
    <title> Functions</title>
</head>
<body>

<?php
//Calculate volume
if(isset($_POST["submit"])){
   $r = (float) $_POST['radius'];
   $h = (float) $_POST['height'];

    function volumeCylinder($r,$h)
    {
    $pi = 3.141592653589;
    $vol = $pi * $h * pow($r,2);
    return $vol;

    }
    echo "The volume is : " .volumeCylinder($r,$h);
    }
echo "<br />";

//Calculate Area
if(isset($_POST["submit"])){
   $ra = (float) $_POST['radius'];
   $ha = (float) $_POST['height'];

   function areaCylinder($ra,$ha)
   {
   $pi = 3.141592653589;
   $cylinder_area = 2 * $pi *($radius * $height);
   return $cylinder_area;

   }
   echo "The area is : " .volumeCylinder($ra,$ha);

   }
?>

</body>
</html>
Member Avatar

diafol

If the radius = 2, then vol = area.

I think the error was echo "The volume is : " .volumeCylinder($r,$h);
I changed it to
echo "The area is : " .areaCylinder($ra,$ha);
but I got those error ???

The volume is : 6283.185307178

Notice: Undefined variable: height in C:\xampp\htdocs\test\functions.php on line 33

Notice: Undefined variable: radius in C:\xampp\htdocs\test\functions.php on line 33
The area is : 0

Thank so much diafol
This line was wrong and I fixed it
$cylinder_area = 2 * $pi *($ra * $ha);

Member Avatar

diafol

You should be able to use M_PI constant for pi - no need to hard code it.