Today's Lesson: PHP 3-Day Foundation Consolidation video Tutorial "Yan 18"
1. Recursive concept
function sum ($n) {if ($n = = 1) {return 1;} return $n + sum ($n-1);} echo sum (+); echo "<br/>";
2. Recursion Skills
Recursive techniques/assumptions-assuming that your function has been completed//recursively prints all file directories and subdirectories under the current directory function Printdir ($a, $lev = 1) {$DH = Opendir ($a); while ($row = Readdir ($DH)) !== false) {echo str_repeat (' ', $lev), $row, ' <br/> '; if ('. ' = = $row | | ‘..‘ = = $row) {continue;} if (Is_dir ($a. '/'. $row)) {Printdir ($a. '/'. $row);}} Closedir ($DH);} $a = '. '; Printdir ($a, $lev);
3. Static variable
$arr = Array (1,2,3,array (4,array (5,6))),//write recursive function, calculate all the cells and function sun ($arr) {Static $sum = 0;foreach ($arr as $v) {if (is_ Array ($v)) {Sun ($v);} else {$sum + = $v;}} echo $sum, ' <br/> ';//11,4,6return $sum;} Echo Sun ($arr);
Rest
This article from "A Big big waste fish" blog, declined reprint!
Abandoned fish--how long does PHP take to get from getting started to giving up? 15