Recursion

In programming, we use the term "recursion" for an algorithm (some computer code) that is able to call itself. We also speak of "self-referencing". PHP functions can call themselves recursively, like the example below which adds up the first X integers (just as an example, as this can be computed faster with x*(x+1)/2).

// calculation of : x + (x-1) + ... + 3 + 2 + 1
function sum($x) {
    if ($x <= 0) return 0;
    return $x + sum($x-1);
}
// call it
$s = sum(8);

SPIP also allows you to write recursive loops within the templates.

Author Mark Baber Published : Updated : 02/06/10

Translations : English, français