Jump to content

Help with calculating and return


kakes

Recommended Posts

Hello all!

Got another code that I need a helpful nudge with. I need to create a function to calculate the area of a circle. The function has to take in two arguments (a number and a string). The number is the diameter; the string is the word "diameter." The function has to return the area of the circle or a -1 for an error. The script has to initialize test variables, call the function and display the results. I'm such a noob so don't laugh when you see my code thus far. I appreciate any help you can offer.

 

<? php
// variables
$diameter = 4.0;
$pi = 3.14;
$area = ($diameter * $pi);
$title = "Circle";
         
function circle_function (4.0, "diameter")
          if ($diameter * $pi) !=$area)
       {
         return $area;
        }
         
         {
         else 
         return -1; 
         }
         if ($area == -1)
         {
         echo "Invalid input";
         exit -1;
         }
         
         echo "<html>
	<head>
		<title>
		$title
		</title>
	</head>
<body>
   <h1>Area of a Circle</h1>\n";
      echo circle_function();
   
   echo    "</body>
		</html>";
?>

Link to comment
https://forums.phpfreaks.com/topic/225683-help-with-calculating-and-return/
Share on other sites

Okay, I've worked really hard on this and almost have it. There's just one small thing that I can't get to work right. Here's my new code.  :-\

 

<?php
function circleArea($total, $area, $diameter)
     {
         // Calculate area of circle or returns a -1 if area is not diameter x pi
         if ($area == "Area")
            {
               $answer = $diameter * $pi;
               $answer .= "Area";
            } 
         elseif ($radius == "Diameter")
            {
                $answer = $raduis * $radius;
                $answer .= "Diameter";
            }
         else
            {
                $answer = -1;
            }
         return $answer;
     }

$title = "Calculate Area of Circle";
$diameter=4.0;
$radius=2.0;
$pi=3.14;
$total=$diameter * $pi;

$total = circleArea($area, "Total", $diameter);
$area = circleArea($total, "Area", $area);
$error = circleArea(1, "Stuff", 10);



echo "<html>
   <head>
      <title>
         $title 
      </title>
   </head>
   <body>
       <h1>
          $title
       </h1>
       <h3>
          If the diameter of a circle is $diameter the total area is $total.
       </h3>

   </body>
</html>\n";
                      
?>

You have a few scope issues. Variables like $pi, $radius, etc.. that are defined outside of the function and aren't passed in won't be accessible in that context (You don't even need to define $pi yourself, you can use the predefined constant M_PI - it will be more accurate anyway).

 

You've named things in a strange way that don't really make it clear as to what your intentions are; For example, "if($radius == "Diameter")", that doesn't really make sense, and I'm not sure what you're trying to accomplish with that.

 

You also seem to have some problems with your formulas. For example, the area of a circle is: [imath]A = \pi r^2[/imath] (you can do exponents in PHP using the pow function).

Your description of what you need to do doesn't seem complete, or consistent with your example. Why do you need to take a parameter "diameter"? Where should that parameter be used?

 

A simple example of what it sounds like you want:

 

function area_from_diameter($diameter, $str) {
if($diameter < 0) {
	return -1;
}
return M_PI * pow($diameter / 2, 2);
}

$diameter = 5;
echo area_from_diameter($diameter, "diameter");

I've been working on this as you'll see but it still doesn't work quite right. Any suggestions? Thanks!!

 

<?php
// variables
$diameter = 6;
$pi = 3.14;
$area = ($diameter * $pi);
$title = "Circle";
        function circle_area($num,$type="radius")
{	
if(is_numeric($num) && is_string($type) && !empty($num) && !empty($type))	
{		
$radius = ($type == "diameter") ? $num/2 : $num; // Radius is set to half the given number if the type is diameter, or unchanged if radius		
$area = pi()*pow($radius,2); // Pi as defined by php multiplied by radius to the power of 2 (squared)		
return $area; // Return result	
}	
else	
{		return -1; // Return Pi when invalid/empty values are given	
}
}
        
        echo "<html>	
        <head>
        <title>			
            $title			
         </title>		
        </head>	
         <body>   
            <h1>Area of a Circle</h1>\n";      

         echo circle_area(6,"diameter");      
         echo    "</body>			</html>";
?>

 

I know, I need to clean up my code.  ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.