somenewbie Posted June 1, 2010 Share Posted June 1, 2010 hello everyone, i made a function that will check if a file with specific name already exists in a directory and if not it will keep on looking for the next possible name by adding incremented number to the end of the name. and i believe that i found a bug in php but did not want to report it before having someone else checked too i used recursion function header: fetch_next_freefile($where,$name,$num=0) -where is the location of the file, name is the name and num keeps incrementing while checking for next unused filename however, when i call the function recursively (inside definition) like this: fetch_next_freefile($where,$name,$num++) -it doesn't work. it will reset connection on loading instead when i call the function recursively like this: fetch_next_freefile($where,$name,($num+1)) -it will work does anybody know why? can someone try my function out? version that will work: function fetch_next_freefile($where,$name,$num=0){ $basename=substr($name, 0, strrpos($name, '.') ); $extension=substr($name, strrpos($name, '.') ); $checkme=false; if($num==0){ if(is_file($where.$basename.$extension)){ $checkme=true; } }else{ $checkme=true; } if($checkme){ if(is_file($where.$basename.$num.$extension)){ return fetch_next_freefile($where,$name,($num+1)); }else{ return($where.$basename.$num.$extension); } }else{ return $name; } } echo(fetch_next_freefile('..directory where file is located..','file name')); version that won't work: function fetch_next_freefile($where,$name,$num=0){ $basename=substr($name, 0, strrpos($name, '.') ); $extension=substr($name, strrpos($name, '.') ); $checkme=false; if($num==0){ if(is_file($where.$basename.$extension)){ $checkme=true; } }else{ $checkme=true; } if($checkme){ if(is_file($where.$basename.$num.$extension)){ return fetch_next_freefile($where,$name,$num++); }else{ return($where.$basename.$num.$extension); } }else{ return $name; } } echo(fetch_next_freefile('..directory where file is located..','file name')); of course, check for a file that exists in your directory, and the following one as well, so it would go into recursion. so for example, have two files called "a.jpg" and "a0.jpg" in a directory and check for file "a.jpg" Quote Link to comment https://forums.phpfreaks.com/topic/203568-php-cant-increment-in-recursion/ Share on other sites More sharing options...
kenrbnsn Posted June 1, 2010 Share Posted June 1, 2010 You have to know how "++" works. When it is after a variable, $i++, the variable is used then incremented. When it is before the variable, ++$i, the variable is incremented then used. You can see this in this example: <?php $n = 1; echo '$n++ = ' . $n++ . "<br>\n "; echo '$n = ' . $n . "<br>\n"; $m = 1; echo '++$m = ' . ++$m . "<br>\n"; echo '$m = ' . $m . "<br>\n"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/203568-php-cant-increment-in-recursion/#findComment-1066337 Share on other sites More sharing options...
dabaR Posted June 1, 2010 Share Posted June 1, 2010 I think a while loop instead of recursion in this case would make more sense, but that's just me. Try ++$num for the thing your question was about. Although Ken beat me to the punch. Quote Link to comment https://forums.phpfreaks.com/topic/203568-php-cant-increment-in-recursion/#findComment-1066338 Share on other sites More sharing options...
jcbones Posted June 1, 2010 Share Posted June 1, 2010 Let me explain this way. $n = 0; echo $n++; // 0 = echo $n then increment the number. echo $n; //1 = echo $n echo ++$n; // 2 = echo $n AFTER incrementing the number. Durn Ken beats everyone to the punch. Quote Link to comment https://forums.phpfreaks.com/topic/203568-php-cant-increment-in-recursion/#findComment-1066341 Share on other sites More sharing options...
somenewbie Posted June 1, 2010 Author Share Posted June 1, 2010 oooooooh, damn. i totally forgot about that. so like that it would just keep running in loop.. thanks for the answers people edit: if anyone wanted to use that function feel free to do so but i forgot a little thing, in one of the cases it should return ($basename.$num.$extension); not ($where.$basename.$num.$extension); Quote Link to comment https://forums.phpfreaks.com/topic/203568-php-cant-increment-in-recursion/#findComment-1066351 Share on other sites More sharing options...
ignace Posted June 2, 2010 Share Posted June 2, 2010 I think a while loop instead of recursion in this case would make more sense, but that's just me. No, you are right. Quote Link to comment https://forums.phpfreaks.com/topic/203568-php-cant-increment-in-recursion/#findComment-1066592 Share on other sites More sharing options...
somenewbie Posted June 10, 2010 Author Share Posted June 10, 2010 I think a while loop instead of recursion in this case would make more sense, but that's just me. No, you are right. hm, and why? Quote Link to comment https://forums.phpfreaks.com/topic/203568-php-cant-increment-in-recursion/#findComment-1070583 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.