kakes Posted January 26, 2011 Share Posted January 26, 2011 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/225683-help-with-calculating-and-return/ Share on other sites More sharing options...
kakes Posted January 26, 2011 Author Share Posted January 26, 2011 Is it that bad? I've been working on this for days and I'm going nuts. Maybe it'll click for me soon. :'( Quote Link to comment https://forums.phpfreaks.com/topic/225683-help-with-calculating-and-return/#findComment-1165306 Share on other sites More sharing options...
kakes Posted January 26, 2011 Author Share Posted January 26, 2011 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/225683-help-with-calculating-and-return/#findComment-1165549 Share on other sites More sharing options...
Alex Posted January 26, 2011 Share Posted January 26, 2011 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). Quote Link to comment https://forums.phpfreaks.com/topic/225683-help-with-calculating-and-return/#findComment-1165558 Share on other sites More sharing options...
kakes Posted January 26, 2011 Author Share Posted January 26, 2011 I just don't understand what I'm supposed to do. I went by an example but, apparently, I didn't quite get what it was all about. Thanks for looking. Quote Link to comment https://forums.phpfreaks.com/topic/225683-help-with-calculating-and-return/#findComment-1165635 Share on other sites More sharing options...
Alex Posted January 26, 2011 Share Posted January 26, 2011 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"); Quote Link to comment https://forums.phpfreaks.com/topic/225683-help-with-calculating-and-return/#findComment-1165640 Share on other sites More sharing options...
kakes Posted January 26, 2011 Author Share Posted January 26, 2011 I'm probably overcomplicating it like I do everything else. Thank you for your help. Your example calculates like I need it to. I hope I can wrap my brain around this soon. Quote Link to comment https://forums.phpfreaks.com/topic/225683-help-with-calculating-and-return/#findComment-1165660 Share on other sites More sharing options...
Alex Posted January 26, 2011 Share Posted January 26, 2011 Well I don't think it accomplishes the task you're after completely. The second parameter isn't even used. Quote Link to comment https://forums.phpfreaks.com/topic/225683-help-with-calculating-and-return/#findComment-1165669 Share on other sites More sharing options...
kakes Posted January 28, 2011 Author Share Posted January 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/225683-help-with-calculating-and-return/#findComment-1166409 Share on other sites More sharing options...
kakes Posted January 30, 2011 Author Share Posted January 30, 2011 I think I've got it figured out, with your help of course! Thanks for helping!! Quote Link to comment https://forums.phpfreaks.com/topic/225683-help-with-calculating-and-return/#findComment-1167203 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.