Creating Functions

April 13th, 2009 | Tags:

Tutorial Description:

Learn how to create PHP functions.

Functions are used to re-use code. When you are using the same block of code over and over, just create a function, and when you want to update something, you’ll only need to update it once.

The basic syntax is like this:

<?php

function functionname($param1, $param2, $param3)
{

return;
}

The return is inserted at the end to make sure the function will close.

Lets look at a small example, shall we?

Lets say you want to display the recent news, on each page of your site.

<?php

function recentNews()
{
echo
'
<b>Recent News</b>
<p>John Doe is missing again!</p>'
;
}

?>

Now just add the following function call on every page:

<?php

recentNews

();

?>

Whenever you want to add/modify/remove any news, just change it within the function, and it’ll update everywhere.

Keep in mind when you want to call the function, the function definition must be included into that page.

No comments yet.
TOP