Everything PHP: Functions

Published on Jan 27, 2009   //  Development
Off

Everything PHP

This week we’ll be talking about PHP functions. We’ll talk about PHP’s built-in functions and how to create your own.

Buillt-in Functions

PHP has thousands of built-in functions for you to use. You may have already caught onto how to use this, but I’ll explain it anyways. Almost all (if not all) of PHP functions return their result back to you. Off the top of my head, I can’t think of any that will echo their result. There are two types of functions, ones that take no parameters and those that take parameters and somehow compute them into their result. Let’s first look at functions that do not take any parameters:

[code='php']echo time();[/code]

time() is a function that will return the current Unix timestamp. It accepts no parameters. Functions are called by using the function name followed by parentheses in your code. Since most functions will not echo their result, you will use them in variables, constructs or other functions.

Now, let’s look at a function that accepts parameters. Parameters go between the parentheses and should be separated by a comma if you are passing multiple arguments to it.

[code='php']echo date('l');[/code]

Which would output the current day. The date() accepts two parameters, only one of which is necessary.

Custom Functions

There will come a point when you’re programming a large project and will need to utilize the same piece of code over and over again through your project. This is the perfect time to create your own function. Making your own function is fairly simple, and again there are two type, ones with parameters and ones without. We’ll first look at one that doesn’t accept any parameters.

[code='php']function output_copyright() {
$current_year = date('Y');
echo 'Copyright ' . $current_year . ' The Company. All Rights Reserved.';
}[/code]

This function, when called, would echo Copyright 2009 The Company. All Rights Reserved. with the current year.

To create your own function, you need to precede the unique name of your function (it must be unique, and remember function naming rules) by the word function and a space. You then add you parentheses after the function name, and then a block of curly brackets. What you want your function to do should go within these curly brackets. Your function should always either echo or return something (even if it’s just TRUE or FALSE). Once you have created your function, you can only use it within the same document (either above or below the function declaration, it doesn’t matter), unless you include it into another document.

Now, let’s look at a function that accepts some parameters.

[code='php']function output_copyright2( $company, $start_year = NULL ) {
$current_year = date('Y');
if ( !is_null($start_year) ) {
$start_year = $start_year . '-';
}
echo 'Copyright ' . $start_year . $current_year . ' ' . $company . '. All Rights Reserved';
}[/code]

So, if you then called:

[code='php']output_copyright2( 'The Company', '1992' ) ;[/code]

The function would echo Copyright 1992-2009 The Company. All Rights Reserved.. Custom functions that accept parameters are very similar to one that do not accept parameters. The difference is, you put variables within the functions parentheses which act as the parameters you will be accepting within your function. Multiple parameters should be separated with commas, and the order of the parameters will need to be the order you give the parameters to when you’re calling the function. The variables you set in these parentheses will then be the variables you use to get at the provided parameters inside your function. You can also set default values for the parameters by appending an equal sign followed by the default value. However, the parameters with default values must go after the parameters without defaults.

Conclusion

As you can see, custom functions can be very useful. Large PHP projects often end up having hundreds (sometimes thousands) of custom functions to help prevent redundant code. Because we have now gone over functions, you should begin to take a look at the online PHP manual. This manual includes a function reference of all the built-in PHP functions available (as well as some available from extensions). You will end up using the manual all the time while programming with PHP, so you can lookup functions and what parameters they accept, as well as just learn more about PHP.