The Ultimate Guide to PHP Functions

php functions

Functions are what make our code tick. At the end of the day, we’d like our code to actually do something. The function is what makes this happen, it is the verb, the call to action. The building blocks of all great programs are functions. In PHP there are thousands of built in functions, along with the ability to define your own. Since functions are so important to our everyday programming, this tutorial will serve as the ultimate guide to PHP functions. Let’s jump in!

When we program, we need to find a balance between procedure and abstraction. The function is the very beginnings of abstraction. When first setting out to solve a problem, we may not even know how we are going to complete the task. At this early stage, it makes sense to attack the problem with carefully planned steps or procedures. Once we have a workable solution in place, we can put those procedures into a block of code, and create a function with it. A function is this named block of code. It should perform a specific job, and may accept parameters, also called arguments, given to it to perform it’s task. The function will usually return a value. Functions make our code more efficient, and also provide the benefit of saving on compile time. The reason being is that even if we call our function 10 times within a page, it is compiled only once. Another benefit is that, if you need to fix a bug or change logic within it, once it is updated in this one place, all other function calls on this function, whether it be 10 or 100 other instances, will now work correctly. It’s better to update code in one spot versus 100! Lastly, functions help make our code more readable, especially if we give them meaningful names, and isolate their job to a specific task.

We’ll take a close look at the syntax of defining functions, as well as the syntax of calling both built in, and user defined functions.

Call a Function in PHP

No matter if a function was created by you, the awesome programmer, or if the function is already a built in to the language, it will be evaluated in the same way.

$returnvalue = $named_function( [ parameter, ... ] );

A function might accept no parameters at all, or several parameters. Each function is different. The parameters given to the function must be in a specific order, and can be any valid expression, so long as it meets the expectations of the function definition. Passing in parameters that are out of order can result in anything from unexpected results, to a full fledged program crash. By reading the documentation of a function, you will know what it expects.

Examples of Functions


array_unique() is a built in function that removes duplicate entries in an array


substr() is a built in function that allows you to find a substring within a string.


array_unshift() is a built in function that allows you to add a value to the very beginning of an array.

We call a function by simply referencing it’s name. In the first example, an argument is passed to the function array_unique(). This argument is an array, because that is what the function expects to be given. We can see that the array_unique() function accepts an array, removes any duplicates from it, and returns the unique array.

The second example also accepts an argument, but in this case it is of type string. You can see in this example how we can call a function within a function. In this example, strlen() is called within substr(). The returned value of the inner function gets passed to the outter function and then the final result is returned.

Lastly, we make use of the array_unshift() function which takes two parameters, an array, and a value to add to the array.

PHP has a lot of built in functions. Were talking on the order of 5000+ built in functions available for your enjoyment. When starting out, it may be difficult to know where to start. One thing that may help is to determine the most used functions in the language and start there. Since strings and arrays are the most common data types used, it makes sense to start with the most popular php string functions and the most popular array functions. Memorizing this small collection is enough to get you up and running for some basic programming, and help you to debug your various themes and plugins.

Define Your Own Functions

We can define our own functions using the function keyword. Let’s say I want a function that makes it easier to dump the type and contents of a variable to the screen. Here is an example of a function that does just that.

Let’s go over what is happening here. First we use the keyword function, this tell us that the next piece of text is going to be the way we identify or name this block of code for reuse. Second, we can see that the function expects a $value parameter to be passed in. Note that you can assign any name you like to this parameter, it is simply an identifier for what will be passed into the function when it get’s called. In this function, it is not optional, we need this value passed in so the function will run without error. If we try to call the dump() function with no parameters passed in, we will get a scary message that looks like this:

Warning: Missing argument 1 for dump()

We can make a parameter optional by assigning it a value in the function declaration. Let’s see what that would look like:

Now, when we call the dump() function without passing in any arguments, it will not bark at us! When the function is called, it will see that no argument was given, therefore it will assign the provided text from the function declaration to $value, and use that inside the function. On the other hand, if we call the dump() function and pass in a value, the function will ignore the text of the function declaration, and work it’s magic on the value that we pass in, just like when we passed an array of $legocharacters to it. By providing default values to your parameters in the function declaration, you are making your functions more flexible and useful.

Returning a Value

The example above did not return a value, but normally a function would. When we want to return a value from a function, we will use the return keyword. The return statement goes inside the code block of the function. In fact, you can have many return statements inside the function, since you may have some logic or tests that will run, and a value will be returned only when a specific case occurs. Once a return statement is hit during the programs execution, control gets handed back to the calling statement along with the return value. The return keyword can accept any expression, it is quite flexible. You can return $a + $b ( 5 / $c ); just as easily as return $a;. As an example we could have an add function set up like this:

or we could clean things up and simply use this style:

Both of these examples would produce the same return value.

Let’s create another example of a user defined function. In this one we will work some magic on arrays, and specifically we’ll create an array_pluck() function to give us the ability to pluck values from an array:

An array pluck function is very useful in many instances of web development. You may be familiar with the MVC or Model View Controller pattern. When we use MVC and pass data from controllers to views, usually the keys of the data array end up becoming the variable names in the view. This is using the array pluck pattern to make this possible.

We can refactor the first iteration of our array_pluck() function using the built in array_map() function. This will also help us see the return statement in a different light, as well as highlight variable scope in php functions.

Awesome! As we can see this second iteration works just as well 🙂 Note that the return statement is returning a complex expression in this example like we mentioned earlier. Now wait a minute, what is that use($topluck) business all about? Well in PHP, a function uses local scope, also called function level scope. This means that functions keep their own sets of variables that are distinct from any others on the page and also of any other functions. Variables in one function do not have access to variables in another function and vice versa. The variables defined inside a function, including its parameter list, are not available outside the function, and, by default, variables defined outside a function are not accessible inside the function. How do we get around this?

Closures and Anonymous Functions

We can see that the array_map() function inside of the array_pluck() function accepts a nameless function as it’s first parameter. What?! That’s right, not only can you pass a function to a function, you can pass a function that has no name to a function. This is called an anonymous function. In addition to the anonymous function, we specify use of a closure with the use($topluck) construct. Without this, the inner function would not have access to the variables of the outer function, and the function would fail.

Pass by Value vs Pass by Reference

Pass by Value

It is much more common to pass by value than by reference in PHP, but you but there are times when you will need to pass by reference. We’ll show this by creating two versions of a function that should double a value passed in.

This code is using pass by value, which means a copy of the variable is passed in. Therefore when the function runs, it doubles the copy of the variable, not the value of the caller’s variable. Fear not! We can fix this 🙂

Pass by Reference

We are going to do something drastic. We will entirely rewrite the function and use pass by reference now:

Hey, it’s working now! Yes it is working, and you’ll notice all we did was add the & operator to the parameter in the function declaration 🙂 This works because a copy is never made in this case, the variable simply gets passed in. You can almost think of it like a pointer to a memory address. Note that only variables can be supplied to parameters declared as passing by reference. In addition, there is no reference sign on a function call – only on the function definition. Function definitions alone are enough to correctly pass the argument by reference.


We covered a lot in this PHP function tutorial. If you’re in the beginning stages of your PHP journey, read this post many times until the concepts are second nature to you. If you’re already an advanced code slinger, hopefully you found some nice refreshers in this post as well. What’s next? Consider jumping into Object Oriented PHP!