introduction to functions

Javascript Tutorials

Tutorial

Click on thumbnailed images to enlarge

For this tutorial, your files must be .php. I will assume you know what the Header is...

Make a file called functions.php and place the following code in:

<?php
function showheader() {
?>
<PLACE ANY HEADER CODES HERE>

<?php
}
?>


Let's review what I did okay?

<?php

I'm sure you know that this opens the php file right? :happy:

function showheader() {

^ Right here you named the function (in this case, showheader). You have also opened it with {.

?>

^ You wondering why I ended the php script before closing the function huh? Well, if you closed it, you end up using ECHO's (I'll explain in another tutorial) in the <PLACE ANY HEADER CODES HERE> part. And probably end up getting Parse Errors

<PLACE ANY HEADER CODES HERE>

^ In this part, you may place any part in the head that you may want to include, in my case I used the <HTML> tag. You may anything else, even the opening of a table or inclusion of JS files..

<?php
}
?>

^ In this part, you've opened up another PHP tag and closed the function.

How the heck do you display this?

Make a new page and make it index.php or w/e and place the following

<?php
require('functions');
showheader();
?>
<YOU INCLUDE YOUR BODY STUFF HERE>


Step by step again...

require('functions.php');

^ You remember the first file you made? Well, you'll need to "require" that file in order to activate the function.

showheader();

^ Right here you've kinda "included" the bits you placed inside the function. You don't need to close this since there's no { present so yah...

Well, the question on your mind is, WHY CHOOSE THIS OVER INCLUSIONS? Um, it requires less files and places the data in the same file. Plus this is an intro to functions.. You may continue to add more functions in like so:(functions.php)

<?php
function showheader() {
?>
<PLACE ANY HEADER CODES HERE>

<?php
}
function showfooter() {
?>
<PLACE ANY FOOTER CODES HERE>
<?php
}
?>


I've just added a footer function. Now the index.php would look like so:

<?php
require('functions');
showheader();
?>
<YOU INCLUDE YOUR BODY STUFF HERE>
<?php
showfooter();
?>


You may freely post this tutorial on your site without my permission but you must give credit.

If you have any questions or concerns, you may reply to this topic, PM me, or IM over AIM at HiepInDaClub. The reason for the IM is because I'm now an inactive member of createBlog :cry: I am a little busy coding PHP now that I've learned quiet a bit from it.

Tutorial Comments

No comments yet. Be the first one to comment!

Tutorial Details

Author Godsend View profile
Submitted on Feb 11, 2006
Page views 4,567
Favorites 0
Comments 0

Tutorial Tags