Jump to content

Recommended Posts

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  ;D

 

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?

Link to comment
https://forums.phpfreaks.com/topic/125761-making-a-function-for-different-values/
Share on other sites

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);
?>

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.