Recursive Functions are functions that auto-call themself. Generally, a recursive function returns a value that are passed as argument, so the value is passed from an auto-call to the other, until it is returned.
Recursive functions are very useful in equations with factorial numbers and some operatios with multi-dimensional array.
Factorials are written like 6! and this means: 6 * 5 * 4 * 3 * 2 * 1. So 6! is 4320 and 4! is 24.
In the fallowing example we have a recursive function that finds the factorial of a number "$nr" (here 8).

<?php
// this function auto-calls itself (decrementing $nr) until $nr is 0
function factorial($nr) {
  if($nr > 0) $re = $nr * factorial($nr-1);
  else if($nr == 0) $re = 1;

  return $re;
}

echo '8 factorial is: '. factorial(8);        // 8 factorial is: 45360
?>

- To see another example, visit this page: Get all the unique numbers from two-dimensional array.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Recursive Functions in PHP

Last accessed pages

  1. Drag Racer V3 (15437)
  2. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (7033)
  3. PHP PDO - prepare and execute (4934)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (41871)
  5. Fancy Pants (421)

Popular pages this month

  1. Qwop (7896)
  2. Drag Racer V3 (5542)
  3. Bubbles3 (2160)
  4. List with All the Games (1834)
  5. Flash Games - Free online Games (1718)