06c02-1.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 21 行

PHP
21
字号
<?php// A function to add all the numbers from 1 to the number given.// Excuted in this case, with a for loopfunction sum_up_to($number) {    $result = 0;    for ($i = 1; $i <= $number; $i++) {        $result += $i;    }    return $result;}// And now the same function, written as a recursive functionfunction sum_up_recursive($number) {    return ($number) ? ($number + sum_up_recursive($number - 1)) : 0;}// Try this in both cases, with the number 42, which should return: 903echo '<p>Regular version = ', sum_up_to(42), "</p>\n";echo '<p>Recursive version = ', sum_up_recursive(42), "</p>\n";?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?