PHP Functions, An introduction
Functions allow you to store bits of code and call them at appropriate times. Their structure is very similar to JavaScript Functions.
We'll start with a simple function that prints the date to the page. The basic structure of a function is like this:
<?PHP
function printDate () {
}
?>
You only need to open the PHP tag once, even if you have multiple
functions. The structure of one function has the following:
functionthis tells PHP we are about to outline a series of
instructions that will be called at a later dateprintDatethis is the name of the function()parameter brackets that will contain any
additional information which is being passed to the function{opens the command block}closes the command block, and ends the
function
At this point our function doesn't have any procedures to run. We will
add those now:
function printDate () {
// the date() function is a built-in function which will print
// today's date. Please look through the documentation on
// www.php.net to see all of the ways this can be formatted.
$today = date("Y-m-d");
print $today;
}
Now you can call the function from anywhere on your page where you need
to print today's date.
Using the Function
The function would be called inside an event handler, like this:
<?PHP printDate(); ?>
Activity
Use the basic function above to print today's date at the top and bottom of your news page.
- Create a new XHTML page with the .php extension.
- Add the function provided for printing today's date to the very top of
the page (even before the DOCTYPE). - Update the
titleof the page, and add a heading with the
same name. Add news items to the page using proper semantic markup. You
may want to use today's news from CBC, or ./ if you are not feeling creative. Be sure to give credit to your news sources. - Today's date should be printed at both the top, and bottom of the page. Create an appropriate place in the page for this date.
- Open (and close) the PHP tags wherever you want the date to be inserted.
- Inside the PHP tags, call the function. (See "Using the Function" above.)
- Save your page and upload it to the server.
- Test your function by calling up your page in a web browser.
- Modify your function so that it prints the date using a different format. Go to php.net. Do a search for date and read through each of the options. There are a LOT! Look for the following options:
- day of the week (e.g. Tuesday).
- name of the current month (e.g. November).
- one digit month day (e.g. 9)
- two digit month day (e.g. 09)
- two more that are of interest to you
Want More Tutorials?
Are you interested in getting even more tutorials sent to your inbox? Sign up for our incredibly popular tips mailing list and get a free tutorial every month. You may even win a coupon for a free e-book (include your whole name if you want to win stuff).
For a limited time we'll even send you a bonus tips sheet The Top Ten Mistakes People Make when Theming Drupal when you sign up for the list.
