Skip to content

Instantly share code, notes, and snippets.

@jaceju
Created June 14, 2019 02:41
Show Gist options
  • Save jaceju/4e028cc6e8c97391ce8d03d1fcdc1183 to your computer and use it in GitHub Desktop.
Save jaceju/4e028cc6e8c97391ce8d03d1fcdc1183 to your computer and use it in GitHub Desktop.
Static variable in PHP function
<?php
function f($n) {
static $result = [];
if (isset($result[$n])) {
return $result[$n];
}
if ($n <= 2) {
$r = $n;
} else {
$r = f($n - 1) + f($n - 2);
}
$result[$n] = $r;
return $r;
}
echo f(40);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment