search

Home  >  Q&A  >  body text

I don’t know much about callbacks. How does the second 1 come from the beginning, and the parameters after 3. I drew a picture, but I still can’t imagine how it works.

<?php
function fibonacci($n){
if($n==1 ||$n==2){
return 1;
}else{
return fibonacci($n-1)+fibonacci($n-2);
}
}


for($x=1;$x<=10;$x++){
if ($x==1){
echo '0,';
}
if ($x!=10){
echo fibonacci($x).',';
} else {
echo fibonacci($x);
}
}
?>



HUNTHUNT2658 days ago1103

reply all(2)I'll reply

  • 风豆丁

    风豆丁2017-08-23 22:44:31

    This is called recursion, not callback

    Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13....

    The first number, the second value is 1, this is a rule of.

    Starting from the third number, the value of the current number is the sum of the previous two numbers. This is the inherent law of the Fibonacci sequence.

    Use recursive thinking to find the value of the nth number: fibonacci($n) = fibonacci($n-1)+fibonacci($n-2);

    reply
    0
  • HUNT

    HUNT2017-08-21 06:45:50

    The picture is based on my understanding, I hope it can help you understand better what my problem is

    未命名.jpg

    reply
    0
  • Cancelreply