crashmaster Posted March 17, 2008 Share Posted March 17, 2008 Hi there, I have function, which gets $path to file, if file doesnt exist, it goes 1 level up... But this function doesnt work. why ?? function _path ($path) { if (!file_exists($path)) { _path('../'.$path); } else { return $path; } } Link to comment https://forums.phpfreaks.com/topic/96603-recursive-function/ Share on other sites More sharing options...
papaface Posted March 17, 2008 Share Posted March 17, 2008 _path() isn't a PHP function. Link to comment https://forums.phpfreaks.com/topic/96603-recursive-function/#findComment-494347 Share on other sites More sharing options...
crashmaster Posted March 17, 2008 Author Share Posted March 17, 2008 why function doesnt work, even if I change the name of the function! Link to comment https://forums.phpfreaks.com/topic/96603-recursive-function/#findComment-494350 Share on other sites More sharing options...
lemmin Posted March 17, 2008 Share Posted March 17, 2008 You are doing the recursion wrong. For recursion to work, it has to return the return value of the internal call. Your functions are working, but the return value is getting returned to the first call and nothing is done with it. Add the word return to your code and it should work fine: function _path ($path) { if (!file_exists($path)) { return _path('../'.$path); } else { return $path; } } Link to comment https://forums.phpfreaks.com/topic/96603-recursive-function/#findComment-494357 Share on other sites More sharing options...
Orio Posted March 17, 2008 Share Posted March 17, 2008 Forgot a return there... <?php function _path ($path) { if (!file_exists($path)) { return _path('../'.$path); } else { return $path; } } ?> Keep in mind that this function (theoretically) will never stop if there's no such file name in the current folder and all the folders above it. You should add a limit. Example: (No need to change anything in the function call, $call is an optional parameter) <?php function _path ($path, $calls = 0) { $calls ++; if($calls > 15) return FALSE; if (!file_exists($path)) { return _path('../'.$path, $calls); } else { return $path; } } ?> Orio. Link to comment https://forums.phpfreaks.com/topic/96603-recursive-function/#findComment-494365 Share on other sites More sharing options...
papaface Posted March 17, 2008 Share Posted March 17, 2008 why function doesnt work, even if I change the name of the function! lol sorry I didn't realise that was the name of your function. Link to comment https://forums.phpfreaks.com/topic/96603-recursive-function/#findComment-494367 Share on other sites More sharing options...
crashmaster Posted March 17, 2008 Author Share Posted March 17, 2008 LOL! __ I am author of this topic, But I cannot make it SOLVED.. why ??? Server error ? Link to comment https://forums.phpfreaks.com/topic/96603-recursive-function/#findComment-494373 Share on other sites More sharing options...
papaface Posted March 17, 2008 Share Posted March 17, 2008 The solved mod has been removed I think (for regular users anyway). Link to comment https://forums.phpfreaks.com/topic/96603-recursive-function/#findComment-494379 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.