Coding with SPIP 3.0
SPIP3.0 Technical Documentation

> > > Recursion

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).

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

Download

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

  • Author :
  • Published :
  • Updated : 02/06/10
  • Translations : English, français