richrock Posted September 25, 2008 Share Posted September 25, 2008 Hi, I'm really loving php - I started my programming life with it and it's great! The help I've had here has been brilliant thus far - thanks! One day I hope to return the favours Now I'm getting into writing functions, and I've got four that perform a percentage calculation! Obviously not getting this... I've noticed reading php.net and various other teaching guides that functions stop the repeating of code, and here I am with : function getTwenty($total) { $twenty = $total * 0.20; return $twenty; } function getFive($total) { $five = $total * 0.05; return $five; } function getVAT($commission) { $complus = $commission * 0.175; return $complus; } function getOnePointFive($hammer) { $warranty = $hammer * 0.015; return $warranty; } How can I reuse this function for each percentage, thus reducing the repeated code into one function? Also, the percentage for commission (getTwenty()) will be a variable amount, so I need to figure that too... Am I right in thinking that there would be some if statements - like if ($hammer) { calculate 0.015 } and so on? Quote Link to comment https://forums.phpfreaks.com/topic/125761-making-a-function-for-different-values/ Share on other sites More sharing options...
Zane Posted September 25, 2008 Share Posted September 25, 2008 well....there is no reason whatsoever to make a function to do percentages....but I can see you're just earning and that's always good. To help you out..this could consolidate it all. function doPercent($p, $x) { switch($p) { case 20: $val = $x * .2; break; case 5: $val = $x * .05; break; case 'VAT': $val = $x * .175; break; case 1.5: $val = $x * .015; break; default: $val = $x * $p; break; } return $val; } $someNumber = 5000403; $getTwenty = doPercent(20, $someNumber); $getFive = doPercent(5, $someNumber); $getVAT = doPercent("VAT", $someNumber); $getOnePointFive = doPercent(1.5, $someNumber); $getForty7 = doPercent(.47, $someNumber); ?> Quote Link to comment https://forums.phpfreaks.com/topic/125761-making-a-function-for-different-values/#findComment-650319 Share on other sites More sharing options...
richrock Posted September 25, 2008 Author Share Posted September 25, 2008 well....there is no reason whatsoever to make a function to do percentages....but I can see you're just earning and that's always good. Sorry - I'm a little confused by this statement. Does this mean I've misunderstood the purpose of functions to cut down on unecessary repetition? I would have thought a function to perform as many percentage calcualtions (adn I've got about 5 pages of them!!!) would have helped... ??? I'll try this out though... Looks like what I needed, and see how it works Thanks Quote Link to comment https://forums.phpfreaks.com/topic/125761-making-a-function-for-different-values/#findComment-650332 Share on other sites More sharing options...
Zane Posted September 25, 2008 Share Posted September 25, 2008 well....there is no reason whatsoever to make a function to do percentages....but I can see you're just earning and that's always good. Sorry - I'm a little confused by this statement. Does this mean I've misunderstood the purpose of functions to cut down on unecessary What I meant was that there's no reason to create a function to do something that small. you can just as easily do $getTwenty = $somevariable * .2 there's really not point in it. Now if you wanted to take 20 percent of it and then calculate that onto something else...and use that result to create an image and use ..certain data you get to return a name or some HTML...then I'd recommend making a funciton. Functions are meant to take input and put it into output that would otherwise take too many lines of procedural code. Say for instance you wanted to be able to just say creaepage("blue") and out pops and entire page already created in a blue format. Or if you wanted to get the statistics of one person by just their name getStats("ralph") and out pops an entire table full of all the information you want. That's how functions are meant to work. Of course most of the time these kind of functions are built within classes but that's a different story altogether (see OOP). But that's the jist of it Quote Link to comment https://forums.phpfreaks.com/topic/125761-making-a-function-for-different-values/#findComment-650376 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.