O'Reilly logo

Functional PHP by Gilles Crettenand

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Recursion, stack overflows, and trampolines

We first presented recursion as a possible solution to programming problems in Chapter 3, Functional basis in PHP. Some memory complications were hinted at; it is time to investigate those further.

Tail-calls

A call to a function that is the last statement done before returning a value is called a tail-call. Let's have a look at some examples to get a grasp of what it means:

<?php 
function simple() { 
    return strtoupper('Hello!'); 
} 

This is without any doubt a tail-call. The last statement of the function returns the result of the strtoupper function:

<?php 

function multiple_branches($name) { 
    if($name == 'Gilles') { 
        return strtoupper('Hi friend!'); 
    } 
    return strtoupper('Greetings'); 
} 

Here, both calls to the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required