# Need help with calculating and return 

**URL:** <https://www.phphelp.com/t/need-help-with-calculating-and-return/12549>\
**Category:** General PHP Help\
**Created:** [January 26, 2011, 1:28am UTC](https://www.phphelp.com/t/need-help-with-calculating-and-return/12549 "2011-01-26T01:28:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jumper](https://www.phphelp.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jumper](https://www.phphelp.com/u/jumper)\
**Post date:** [January 26, 2011, 1:28am UTC](https://www.phphelp.com/t/need-help-with-calculating-and-return/12549/1 "2011-01-26T01:28:23Z")

</div>

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.

```auto
<? 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>";
?>
```

---

<div class="post-metadata">

**Author:** ![jumper](https://www.phphelp.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jumper](https://www.phphelp.com/u/jumper)\
**Post date:** [January 26, 2011, 3:01am UTC](https://www.phphelp.com/t/need-help-with-calculating-and-return/12549/2 "2011-01-26T03:01:30Z")

</div>

My code didn’t copy exactly right but it’s close. Really bad, huh? Sorry. Maybe with a little nudge, I’ll get it! ;D

---

<div class="post-metadata">

**Author:** ![smokeys\_13](https://www.phphelp.com/user_avatar/www.phphelp.com/smokeys_13/32/212_2.png) [@smokeys\_13](https://www.phphelp.com/u/smokeys_13)\
**Post date:** [January 26, 2011, 10:25pm UTC](https://www.phphelp.com/t/need-help-with-calculating-and-return/12549/3 "2011-01-26T22:25:48Z")

</div>

Hi there,

I’ve quickly thrown together an example of how you might go about this - let me know if you need help understanding/modifying it.

[php]  
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  
}  
}  
[/php]

---

<div class="post-metadata">

**Author:** ![jumper](https://www.phphelp.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jumper](https://www.phphelp.com/u/jumper)\
**Post date:** [January 26, 2011, 10:39pm UTC](https://www.phphelp.com/t/need-help-with-calculating-and-return/12549/4 "2011-01-26T22:39:37Z")

</div>

Thank you so much for the reply!

I put the code in notepad++, added the php tags, saved it as php document, and uploaded it to a server but it wouldn’t display anything.

Did I do something wrong?

Thank you again. I was about to think no one would help me. ☹

---

<div class="post-metadata">

**Author:** ![smokeys\_13](https://www.phphelp.com/user_avatar/www.phphelp.com/smokeys_13/32/212_2.png) [@smokeys\_13](https://www.phphelp.com/u/smokeys_13)\
**Post date:** [January 27, 2011, 6:51pm UTC](https://www.phphelp.com/t/need-help-with-calculating-and-return/12549/5 "2011-01-27T18:51:15Z")

</div>

No problem.

That is just a function you will need to call it to get an output.

Simply put this, for example, inbetween the body tags somewhere:  
[php]echo circle\_area(6,“diameter”);  
[/php]

Or of course you can pass it into a variable and apply any rounding/trimming/validation you want.

---

<div class="post-metadata">

**Author:** ![jumper](https://www.phphelp.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jumper](https://www.phphelp.com/u/jumper)\
**Post date:** [January 28, 2011, 2:35am UTC](https://www.phphelp.com/t/need-help-with-calculating-and-return/12549/6 "2011-01-28T02:35:57Z")

</div>

It’s still all just Greek to me. I think I’m sort of getting a grip on it and then I find out I’m not.

I’m going to paste the code again because, apparently, I’ve put something somewhere it doesn’t belong. Still won’t display in the browser. ☹

I checked the syntax in Putty and it had some issues but I couldn’t find them.

Thank you for your help again. I really, really appreciate it so much.

```auto
<?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>";
?>
```

---

<div class="post-metadata">

**Author:** ![smokeys\_13](https://www.phphelp.com/user_avatar/www.phphelp.com/smokeys_13/32/212_2.png) [@smokeys\_13](https://www.phphelp.com/u/smokeys_13)\
**Post date:** [January 28, 2011, 7:22pm UTC](https://www.phphelp.com/t/need-help-with-calculating-and-return/12549/7 "2011-01-28T19:22:53Z")

</div>

Hi,

You need to include another curly brace after the else. That should fix the problem.

---

<div class="post-metadata">

**Author:** ![jumper](https://www.phphelp.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jumper](https://www.phphelp.com/u/jumper)\
**Post date:** [January 28, 2011, 11:01pm UTC](https://www.phphelp.com/t/need-help-with-calculating-and-return/12549/8 "2011-01-28T23:01:12Z")

</div>

Didn’t work. ☹

Thanks for trying.

---

<div class="post-metadata">

**Author:** ![jumper](https://www.phphelp.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jumper](https://www.phphelp.com/u/jumper)\
**Post date:** [January 31, 2011, 1:55am UTC](https://www.phphelp.com/t/need-help-with-calculating-and-return/12549/9 "2011-01-31T01:55:27Z")

</div>

This task is solved! Thanks for all your help!!
