Today we are going to learn about a thing called a function in the scripting language of PHP.
Here are a few things about functions:
A function is a block of code that can be executed whenever we need it.
Creating PHP functions:
All functions start with the word "function()"
Name the function - It should be possible to understand what the function does by its name. The name can start with a letter or underscore (not a number)
Add a "{" - The function code starts after the opening curly brace
Insert the function code
Add a "}" - The function is finished by a closing curly brace
ok!
So lets start now with a function for echoing todays date!
PHP Code:
<?php
function date()
{
echo date("d/m/Y");
}
?>
So there we go!
That is our first ever function...
Let's just explain that a little..
function date() - tells the server that your creating a function called date
{ -Tells the server to start accepting the code for the function.
echo date("d/m/Y"); - Using the PHP date function just to get todays date seperated by a forward slash.
} - Tells the script that the function is finished.
OK!
So there we go thats a function for us..
So now we want to echo our date somwhere on our page..
Here is an example of a HTML page, with todays date in it.
Code:
<table width="500" border="2" cellspacing="1" cellpadding="0">
<tr>
<td>News added by: Base </td>
</tr>
<tr>
<td><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus sapien nunc, varius ut, bibendum ac, porttitor eu, magna. Vivamus elit mauris, gravida id, congue non, elementum eleifend, augue. Integer iaculis mauris non felis. Morbi mi magna, ullamcorper nec, aliquam id, pellentesque sed, lorem. Curabitur convallis fermentum augue. Donec semper iaculis est. Donec nibh. Suspendisse semper odio id lorem pulvinar vestibulum. Suspendisse ut leo. Integer iaculis. Sed sagittis lorem eget massa. Suspendisse vulputate mattis sem. Maecenas elementum, libero ut consectetuer pellentesque, nibh nisl varius turpis, sit amet accumsan libero neque in sem. </p></td>
</tr>
<tr>
<td><div align="right">Date Added: <?php date(); ?>
</div></td>
</tr>
</table>
The only thing you really need to look @ in this code is this line:
PHP Code:
<?php date(); ?>
That line just parses the contents of the function called date()
Well hope you learned somthing about functions today fokes!
my apologies for any code faults...
Will be using functions in my next tut to do with some really simple mysql stuff!
Summing up todays lesson:
To start off a funtion in php you do this:
function TheNameOfYourFunctionHere()
{ - This thing tells the server your starting off your function
Your function code goes here
} - This thing tells the server your finishing off your function
To display your function at any point you just do this:
TheNameOfYourFunctionHere();
THINGS TO REMEMBER.
You must use { & } to tell the server your starting and ending your function.
If you save your function in a seperate file (e.g. functions.php) then you HAVE to remmeber to do this tag:
<?php include("your-file.php"); ?>
More on that later!
Chow peeps