Jump to content

recursive function


revepastrop

Recommended Posts

Hello!

Maybe I'm a bit ashamed to question some little kind of thing, but I tested 3 hours and couldn't let this function. It's like the most easiest possible recursiv function...:

 

function recursiv($s){
  $s.="b";
  if(strlen($s)>30){return $s;}
  //else return 0;
  else recursiv($s);
}
echo recursiv("h");

 

The result is a blank page...

 

Thanks a lot for your help !!

Link to comment
Share on other sites

@xyph: I personally don't see $b anywhere at all, read properly next time :)

 

Infact he wasn't returning the function if the string was less than 30 characters, simple mistake.

 

function recursiv($s){
  $s.="b";
  if(strlen($s)>30){
    return $s;
  } else {
    return recursiv($s);
  }
}
echo recursiv("h");

 

Regards, PaulRyan.

Link to comment
Share on other sites

To explain the problem:

 

Each time you call the function it is within context of the last time you called the function. When the function returns something it is returning it to the instance from where it is called. So, on the 30th iteration of your original function it was returning the value of $s (a 31 character string). But, it was returning that string to the 'instance' where it was called, which was the 29th iteration of the function. Since the function didn't expect any value to be returned in the else condition the returned value wasn't used.

 

With the change PaulRyan made, the 30th iteration of the function will return the value to the 29th iteration, which will then return that value to the 28th and so on until the value is returned to the original line that called the function - the echo line. Recursive functions have their place, but in many instances there are usually better alternatives. Although the above is obviously just an exercise, for something like that a class would be better suited, IMHO. You can then call the function recursively and apply the changes to a property of the class and not need to continually pass the value to each call of the function.

Link to comment
Share on other sites

THANKS a lot ! Actually I didn't understood very well how recursiv Functions functions. Now with your correction PaulRyan and and your explanation MjDamato, I'm perfect ! Very well, Thanks a lot !

I don't close the topic because maybe I'll ask you about my real Function (Parsing a hierarchical JSON).

 

Link to comment
Share on other sites

So, I wrote this functions. It automatically parse a whole json_decode() -> both as Object and as Array (see commentary).

I searched a lot on the web and didn't find this. It could be useful for others what do you think?

 

//$object=json_decode($json_string)
//PHP json_decode Parser. In this exemple, the Datas will be put in a string 
function parse_json_object($object,$level=0){
  $string="";
  $level++;
  $level_show=$level-1; 
  foreach($object as $key => $value)
  {
    $string.=str_repeat("- \t",$level_show).$key;  //Do what you want with the Keys
    if(is_string($value) || is_float($value) || is_int($value) || is_bool($value)){
      $string.= " : ".$value." (level:".$level_show.")</br>\n"; //Do what you want with the values
    }
    elseif(is_array($value)){
      $string.="[arr] : (level:".$space.")</br>\r";
      $array_level=$level+1;
      $array_level_show=$array_level-1;
      foreach($value as $arrayKey => $arrayValue) {
        $string.=str_repeat("- \t",$array_level_show)."$arrayKey : (level:".$array_level_show.")</br>\r"; //Do what you want with the arrayKeys
        $string.=parse_json_object($arrayValue,$array_level); //use recurse to parse the the objects of the array
      }
    }
    elseif(is_object($value)){
      $string.="[obj] : (level:".$level_show.")</br>\r";
      $string.=parse_json_object($value,$level);  //use recurse to parse the objects of sub Hierarchie
    }      
  }
  return $string;
}

 

and

 

//$array=json_decode($json_string, TRUE)
//PHP json_decode Parser. In this exemple, the Datas will be put in a string 
function parse_json_array($array,$level=0){
  $string="";
  $level++;
  $level_show=$level-1; 
  foreach($array as $key => $value)
    {
      $string.=str_repeat("- \t",$level_show).$key;    //Do what you want with the Keys
      if(is_string($value) || is_float($value) || is_int($value) || is_bool($value)){
        $string.=" : ".$value." (level:".$level_show.")</br>\n";  //Do what you want with the values
      }
      elseif(is_array($value)){
        $string.="[] : (level:".$level_show.")</br>\r";
        $string.=parse_json_array($value,$level);  //use recurse to parse the subarrays
      }  
      elseif(is_object($value)){
        //that schould be not possible
      }
    } 
  return $string;
}

 

Cheers

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.