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; } } Quote Link to comment Share on other sites More sharing options...
papaface Posted March 17, 2008 Share Posted March 17, 2008 _path() isn't a PHP function. Quote Link to comment 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! Quote Link to comment 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; } } Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 ? Quote Link to comment 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). Quote Link to comment 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.