Functions: Creating your own
The PHP functions are great. They do everything you need. However, what happens if you want to do the same thing over and over in a script? For example, in Olate Download 3.0, we want to format the date of the file in the same way every time. We could do:
$formatted_date = date('d/m/Y', $date);
wherever we want to set the formatting. But what if we want to change the way it is formatted in the future? We'd need to edit that line in lots of places throughout the entire application. This would be a pain.
So we created our own function to do this for us. This allows us to use the same code throughout the entire application but if we want to change it, we only need to do so in the function itself, not everywhere it is used. This function is a nice simple example that I shall use here.
The first thing you need to do when creating your own function is declare it. When you do this, you create the name of the function and define which parameters you want to be passed to it. You start the line with:
function
This tells PHP that you are declaring a function. Then you go ahead and give it a name, open brackets ( provide some parameters then close brackets ). So you get
function format_date($date)
You can call the function whatever you like as long as the name is not already used by a PHP function. However, it is common sense to call the function something useful.
function eat_pies($date)
Does not do what you expect. It does not actually have anything to do with eating, or pies...or even eating pies. It will format the date. The same applies with:
function a($date)
That is pointless. You have no idea what that does when you look at it. Similarly, because functions do something, you should try and get a verb in the name. Here we're using the verb 'format'. The function above
eat_pies() uses the verb 'eat'. It just makes it easier to understand what is going on.
Next you need to write the code that will be executed when you call your function:
function format_date($date)
{
$formatted_date = date('d/m/Y', $date);
return $formatted_date;
}
You can see that the code is contained within the curly braces
{ } just like with control structures such as if.
The first line of the function code is our date formatting code. When you call the function in your main script:
format_date('1088768118'); // 1088768118 is a unix timestamp. See the time() and date() functions for info
The argument
1088768118 will be stored in the
$date variable within the function. You can then access that data by using
$date just as you would if you had assigned the value doing:
$date = '1088768118';
The result of the formatting will then be stored in the variable
$formatted_date. The last line then returns that value. As soon as you specify return, the function will stop executing. So:
function format_date($date)
{
$formatted_date = date('d/m/Y', $date);
return $formatted_date;
// We have returned before this
$this = 'will not be set';
}
Now when I call:
$new_date = format_date('1088768118');
The result of the
format_date() function will be stored in the
$new_date function. And if I wanted to echo that directly to the page:
// Will display 02/07/2004
echo format_date('1088768118');
And there you have successfully created your own function. When you call it, it will be executed and the value will be returned for you to use.
You can put your function declarations wherever you like. But the best place is at the top of the file, or in a separate file than you can then include in every page you want to use the function in:
<?php
function format_date($date)
{
$formatted_date = date('d/m/Y', $date);
return $formatted_date;
}
// Will display 02/07/2004
echo format_date('1088768118');
?>