revepastrop Posted October 27, 2011 Share Posted October 27, 2011 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 !! Quote Link to comment https://forums.phpfreaks.com/topic/249942-recursive-function/ Share on other sites More sharing options...
xyph Posted October 27, 2011 Share Posted October 27, 2011 You have not defined $b Quote Link to comment https://forums.phpfreaks.com/topic/249942-recursive-function/#findComment-1282848 Share on other sites More sharing options...
PaulRyan Posted October 27, 2011 Share Posted October 27, 2011 @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. Quote Link to comment https://forums.phpfreaks.com/topic/249942-recursive-function/#findComment-1282849 Share on other sites More sharing options...
Psycho Posted October 27, 2011 Share Posted October 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249942-recursive-function/#findComment-1282850 Share on other sites More sharing options...
revepastrop Posted October 27, 2011 Author Share Posted October 27, 2011 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). Quote Link to comment https://forums.phpfreaks.com/topic/249942-recursive-function/#findComment-1282862 Share on other sites More sharing options...
revepastrop Posted October 29, 2011 Author Share Posted October 29, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/249942-recursive-function/#findComment-1283213 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.