ServersWelcome to the World of PHP Page 10

Welcome to the World of PHP Page 10

ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.




The Function of Functions

So, $val2 will contain 5.

It is not possible to cover all of PHP’s built-in functions as there are so many. But it’s also not necessary, since you understand how to work with a function, you need only consult the PHP function reference to browse available functions and their usage details — what values they accept and what values they return.

We’re more concerned here with coding your own functions, which is not an uncommon thing to do. Let’s say you would like to code an “is a jerk” function — it accepts someone’s name and simply returns their name in the sentence “so-and-so is a jerk.” (This is a very mature example to be sure.)

function is_a_jerk ($name) {
return "$name is a jerk!";
}

A function by itself does not do anything until it is called. We call our new function from within a statement:

print is_a_jerk("Little Johnny Long Pants");

The function does not itself print the result, it simply returns the result; in the above case, this result is then handed to the print function which triggers the output:

Little Johnny Long Pants is a jerk!

And it’s true, he is. Let’s consider a more full-figured example of a homegrown function. Imagine that your function accepts a purchase subtotal and a “ship to” state, and must return a final total incorporating the subtotal, applicable shipping, and applicable tax if the state is one where your store has a physical presence (New York, New Jersey, and West Virginia in this example).

function calcFinal ($subTotal,$shiptoState) {
#First calculate shipping surcharge
if ($subTotal $subTotal += 5;
}
else {
$subTotal += 10;
}

#Setup array of taxable states with tax rates
$taxStates = array ("NY"=>0.075,"NJ"=>0.04,"WV"=>0.035);

#Add state tax
$subTotal *= 1+$taxStates[$shiptoState];

#Done, return results
return $subTotal;
}

Our new function calcFinal accepts two parameters, the first being the subtotal and the second being the state (actually, the two letter state abbreviation is what we’ll use). Using a variation on our previous if statement, the subtotal is boosted with the shipping surcharge. Next, we create an array where each item key is the two-letter abbreviation for a taxable state and each value is the tax rate multiplier (7.5% for NY, for example).

To add sales tax to our subtotal, we add 1 to the tax multiplier and multiply $subTotal by this value, reassigning the result back to $subTotal. Why? If the state were NJ, the subtotal would be multiplied by 1.04. If the state were FL, the array would return a value of zero since there is no key “FL”. We add 1 to this value, therefore multiplying $subTotal by 1, in essence adding no tax.

Finally, the subtotal is returned to the calling expression. The “calling expression” can be any valid expression — imagine that we use an if statement to determine if the total cost exceeds a given threshold and, if so, offering a discount on a future purchase. Suppose that $purchase holds the current value of items purchased:

if (calcFinal($purchase) >= 1000) {
print "Congratulations, you have earned 15% off your next purchase!";
}

Functions are a very important way to segregate and enclose specific tasks, especially tasks which may be called upon repeatedly from different statements in a page.

 


NEXT ->

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends & analysis

Latest Posts

Related Stories