Page 11
Grand Master Functions
How are ya doing? Getting sleepy? We're almost done here, and if you've
followed along this far you're doing great. Have some more coffee or whatever
it is that keeps you perky, and we'll look at functions and then a few final
thoughts.
If you've used HTML a lot, you know that it's a pretty limited language
that was designed back in the dark ages before we all knew what the Net
was capable of.
PHP, however, is very flexible. Not only does PHP have a library of canned
functions that'll do everything from sorting stuff in alphabetical
order to sending email, from connecting with databases to balancing your spaceship's
inertial sub-space dampers, but you can also create your very own functions
to do all manner of things related to your website. The functions you create
get executed exactly like those from the PHP library, but they're your own.
In the following section, I'll show a glimpse of how you create your own
functions and feel the power.
Functions you create are like little machines that do something for you.
You construct them and then call them as needed.
You'll remember that the very first thing we learned to do was a simple
"print" statement, which followed the form:
<?php
print ("whatever it is I want to show up on-screen");
?>
Functions that you create are built upon a similar form, but take it farther:
<?php
function MyFunction ()
{
statements that make up the function;
}
?>
So you start a function with the words function WhatYouNameIt(),
with the words WhatYouNameIt() being anything you choose (no spaces).
Then you define the rules of the function inside the following curly brackets
(that's { and } on lines 5 and 9). Don't you just love the words "curly
brackets?"
Let's walk through the making of a couple functions. Functions come in
two flavors, those that require "arguments" and those that don't.
An "argument" is a variable that comes from outside the function,
but which the function needs in order to run.
Let's first look at one that doesn't require arguments:
<?php
function ChickenMan()
{
print "<b>I am the CHICKEN MAN!</b>";
}
ChickenMan();
?>
line 1: start PHP;
line 3: create function ChickenMan;
line 5: start definition of function ChickenMan;
line 7: definition of ChickenMan is to print "I am the CHICKEN MAN!"
inside <b> and </b> tags;
line 9: end definition of ChickenMan;
line 11: call function ChickenMan (meaning "do the thing that we defined
the function to do");
line 13: close PHP;
Any place in this Web page that you put "ChickenMan();" inside
the <?php and ?> tags, it'll print your little statement. See how
this saves you time if you want to print this string a bunch of times in
different places?
Now let's get a little more complicated and create a function that does take
arguments. Say I'm a loan shark and I'm going to loan you the money to buy
that personal
hovercraft you've been lusting after. I'll lend you however much you
need, but you have to pay me 14 percent interest per week or I break your
legs.
I'll create a function called InterestPayment which calculates your weekly
payments (until you pay off the whole loan amount in one lump sum). If you're interested, take a look at this script in action.
First we'll create a form page where the user enters how much of a loan
he or she wants. The user enters the hovercraft's sticker price, and that
number gets passed along from the form as a variable called $Cost. (For the HTML behind this, go here.)
Next, our function will take the amount that the user enters into the variable
$Cost and spit back 14 percent of that amount, which is how much
the hapless borrower owes every week. This will happen in the PHP page titled
"loanshark.php" that the form page points to (it points with this
code: <form action="loanshark.php" method=post> ). Here's
what the function will look like:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26. |
<html>
<head>
<title>Loans</title>
</head>
<body>
<?php
$interest_rate = .14;
function YouOweMe($cost, $interest_rate) {
$weekly_payment = ($cost*$interest_rate);
print "You better pay me \$$weekly_payment every week, or else!";
}
YouOweMe($cost, $interest_rate);
?>
</body>
</html>
|
Here's the line-by-line breakdown.
line 8: start php;
line 10: set variable called $interest_rate to 14%;
line 12: create function YouOweMe that relates to the variables $cost and
$interest_rate;
line 14: create variable $weekly_payment, the value of which is the cost
times the interest rate;
line 16: print to screen a sentence that uses the value of the $weekly_payment
variable;
line 20: engage the function YouOweMe, which (because of the function defined
beforehand) simply prints the sentence You better pay me [14% of the
amount the user entered on the form page] every week, or else!
Notice on line 16 that we want the code to actually print out a dollar
sign before the weekly payment. We escape that character with a \ to make
sure the PHP engine doesn't think we're naming a variable there. Then we
just happen to name a variable right afterwards ($WeeklyPayment), so the
two together look like \$$WeeklyPayment.
next page»
|