Anonymous and Named Functions in PHP

Discover the differences between anonymous and named functions in PHP with our informative guide. We’ll explain the benefits and drawbacks of each type of function, and show you how to use them effectively in your PHP code. Start optimizing your PHP applications today!

Developing large scaled web applications using PHP can become really hectic really fast if not utilize the most important structures of all, functions. Functions provide us the ability to re-use segments of code instead of writing them over and over again. Using functions we write the code block once and we can call it again again without worrying about chaotic consequences.

Functions, apart from compatibility and re-usability, can enable scalability and maintainability of the code base and sometimes, if used correctly, even better resource management. There are two types of functions in PHP. The ones that we probably know of, are named functions. Functions in programming work a lot like functions in mathematics. We pass input values and we get a corresponding output value.

What are the named functions

A named function contains a label that can be used from the code base in order to call it so it can execute its body of code. Once the function completes the routine of its body, it may return a single value, a set of values or print out the output. Note the function below:

function get_taxed_price($price, $tax) {
    return floatval($price) + (floatval($price) * floatval($tax));

$taxed = get_taxed_price(20, .10);
echo '$' . $taxed; // prints: $22

As a developer, you may need to leverage some library that’s already written for you to use regarding the app and you shouldn’t really care about the details of each specific function. You should only care about what kind of parameters you pass in (if any) and what will the format of the output look like. That’s it. The example illustrates that you only need to provide a price and a tax value to get the complete calculation. The moment you call it, you know that you have the proper value to move on with the logic.

How to use Callback functions

There are functions however that they don’t have a name and can be called only within the confines of another function. Those are called anonymous functions. Several sorting functions in PHP use what we call callbacks. A callback essentially leverages the information of the parent function’s data to provide some conditional modification to the data by overwriting it. Let’s put it into perspective:

$numbers = array(4,2,8,6);
usort($numbers, function($a, $b){
    if ($a==$b) return 0;
    return ($a<$b)?-1:1;

print_r($numbers); // prints: 2,4,8,6

As you can see, the inner function of usort() leverages two elements with each iteration and depending the condition it will keep the corresponding element of choice. That’s the power of anonymous functions.

What is an anonymous function

An anonymous function is a function that has exactly the same behavior as a named function in its core but can be used in many cases as a callback to modify data without redefinitions.

There’s a key aspect however that you must be always mindful of, closures. Closures state that an anonymous function cannot use the values of the outer function’s definition scope and therefore cannot access the addresses directly if not instructed to. In Layman’s terms, you cannot use the variables of the outer function if you don’t show them somehow to the anonymous function. For instance:

function print_taxed_price($price, $tax) {
    return function() use (&$price, &$tax) {
        echo floatval($price) + (floatval($price) * floatval($tax));

$ptp = print_taxed_price(234.34, .14);

Note that the process has two distinctive stages. You define the print_taxed_price() scope by passing the variables of your choice and then you call it by the variable name to get the computed value. Why, you ask? Observe the part use (&$price, &$tax). This statement essentially provides address access of the print_taxed_price() function’s variables to the inner anonymous function. Since we directed the anonymous function to use those two variables, we’re good to go and we don’t create a closure upon definition.

Code Base Branching (CBB)

Functions are easy to use if you can understand visually what they do and how they do it in terms of your overall code. I like to think of functions as branches on a tree. Suppose you have a certain code base already coded, refined and ready to be shipped. The next step is to mark the lines that a function’s being called. Each mark represents a new start of a code branch that will eventually be merged with the main code base depending the end statement. If the end statement is return, that means we have a merger and if there’s none, the branch died.