Search
Close this search box.
Search
Close this search box.

Functions

Creating and Using Simple Functions

As you program, you’ll discover that you use certain sections of code frequently, either within a single script or over the course of several scripts. Placing these routines into a self-defined function can save you time and make your programming easier, especially as your Web sites become more complex. Once you create a function, the actions of that function take place each time the function is called, just as print sends text to the browser with each use.

The syntax for creating a user-defined function is:

function function_name () {
    // statements
}

For example:

function whatever() {
    print 'whatever';
}

You call (or invoke) the function by referring to it just as you do any built-in function. The line of code

whatever(); 

will cause the statement part of the previously defined function—the print command—to be executed.

Creating and Calling Functions That Take Arguments

Although being able to create a simple function is useful, writing one that takes input and does something with that input is even better. The input a function takes is called an argument (or a parameter). This is a concept you’ve seen before: the sort() function takes an array as an argument, which the function then sorts.

The syntax for writing functions that take arguments is as follows:

function function_name($arg1, $arg2, …){
    // statements
}

The function’s arguments are in the form of variables that are assigned the values sent to the function when you call it. The variables are defined using the same naming rules as any other variable in PHP:

function make_full_name($first, $last) {
    print $first . ' ' . $last;
}

Functions that take input are called much like those that don’t—you just need to remember to pass along the necessary values. You can do this either by passing variables:

make_full_name($fn, $ln);

or by sending literal values, as in

make_full_name('Larry', 'Ullman');

or some combination thereof:

make_full_name('Larry', $ln);

The important thing to note is that arguments are passed quite literally: The first variable in the function definition is assigned the first value in the call line, the second function variable is assigned the second call value, and so forth.

Once you’ve defined your own functions, you can place them in an external file and then require that file when you need access to the functions.

Setting Default Argument Values

PHP allows functions to have default argument values: just assign a value to the argument in the function definition:

function printSomething($var = 'world') {
    print "<p>Hello, $who!</p>";
}

Such a function will use the preset values unless it receives a value that then overwrites the default. In other words, by setting a default value for an argument, you render that particular argument optional when calling the function. You’d set an argument’s default value if you wanted to assume a certain value but still
allow for other possibilities:

printSomething(); // returns Hello, world
printSomething('John'); // returns Hello, John

The default arguments must always be written after the other standard arguments (those without default values). This is because PHP directly assigns values to arguments in the order they’re received from the call line. Thus, it isn’t possible to omit a value for the first argument but include one for the second.

To pass no value to a function for a particular argument, use the word NULL (without quotes). This will override the default value, if one is established.

Returning a Value

Functions do more than take arguments; they can also return values.

Doing so requires just two more steps. First, you use the return statement within the function. Second, you use the output somehow when you call the function.

Commonly, you’ll assign the returned value to a variable, but you can also, for example, directly print the output.

Here is the basic format for a function that takes two arguments and returns a value:

function make_full_name($first, $last) {
    $name = $first . ' ' . $last;
    return $name;
}

This function could be used like so:

$full_name = make_full_name($fn, $ln);

There the returned value of the function is assigned to a variable. Here it’s printed immediately:

print make_full_name($fn, $ln);