recursion - Issue with returning value in recursive function PHP -


have issue returning value in recursive function. can echo it. wrong this?

function calculate($i,$count=1) {     $str_i = (string)$i;     $rslt = 1;      ($k=0; $k<strlen($str_i); $k++) {         $rslt = $str_i[$k]*$rslt;     }      if ( strlen((string)$rslt) > 1 ) {         $this->calculate($rslt,++$count);     } elseif ( strlen((string)$rslt) == 1 ) {         return $count;       } } 

in if in code value returned in recursive call not used. don't set value or return it. every call except base case doesn't return value.

try this:

function calculate($i,$count=1) {     $str_i = (string)$i;     $rslt = 1;      ($k=0; $k<strlen($str_i); $k++) {         $rslt = $str_i[$k]*$rslt;     }      if ( strlen((string)$rslt) > 1 ) {         return $this->calculate($rslt,$count+1); // changed line     } elseif ( strlen((string)$rslt) == 1 ) {         return $count;       } } 

now return value returned recursive call. note changed ++$count $count+1 since it's bad style mutate when using recursion.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -