DBGTN.com: [Return to the Previous Page]

• Tutorials: PHP: Functions as Headers

Functions as Headers
-----------------------
PHP is a great language, in this tutorial i will show you how to make a little table header or php command which uses the php function method. This is very useful and time saving so please take notes along the way!

1) first off all we need to tell php that all the stuff we are going to enter next is a function, so open php using the php tags and enter this information:
function name($replacevar = "replace content") {
}
There! i have just titled them so far, next we will move onto making the template.

2) Now we know what a simple function layout looks like lets enter some information, although we don't want it to return nothing when called upon so ill just show you this for now:
function tablehead($title = "Untitled") {
return ("<table>
<tr>
<td>$title</td>
</tr>
<tr>
<td>");
}
3) We have now named the function 'titlehead' and the word in the quotes is the default value for the variable $title, we will call on $title later on in the function as explained below, the title is also editable as explained in step 4. The 'return' command is just like the echo or print command except return is used in functions to get information back to the place where we want it and with the new information. Inside of 'return's quotes and brackets you may NOT use quotes but instead put a backslash ( \ ) before the quote. Note that $title is in there, that will display whatever is in the brackets, and the title is customisable when called upon.

4) I shall now demonstrate how to use the function you just created.
<?php echo tablehead("Info")."<a href=\"power.php\">Power Levels</a></td></tr></table>"; php?>
Where 'tablehead("Info")' is, you can enter anything in the double quotes ( " ) and that will appear in the template we made in step 3 where $title is situated. we use the '.' in between 'tablehead("info")' and ' "<a href=..."; ' to continue the echo string without having to do:
echo tablehead("Info"); echo "<a href...";
You can use anyway you like but i just prefer it like the one shown at the beginning of step 4.

5) Now what this should return in the html source code is:
<table>
<tr>
<td>Info</td>
</td>
</tr>
<tr>
<td>
<a href="power.php">Power Levels</a></td></tr></table>

6) Now you must feel a little more confident so lets try adding some other variable to the function. Study this code:
function tablehead($title = "Untitled", $bgcolor = "ffff00") {
return ("<table>
<tr>
<td bgcolor=$bgcolor>$title</td>
</tr>
<tr>
<td>");
}
We have just added another variable onto the end of title by splitting it with a comma, you can add as many of these as you want. now displaying the other variable you would need to change one minor thing on the echo text in step 4.
<?php echo tablehead("Info","C0C0C0")."<a href=\"power.php\">Power Levels</a></td></tr></table>"; php?>
All we have done is split the echo function by using commas, you should always recall the function variables in order like:
tablehead("before comma","after comma");

That is the end of the tutorial! Later on i will teach you how to use the function to calculate equations and other things!




[Return to the Previous Page]
[Return to the DBGTN homepage]