Jump to content

php can't increment++ in recursion?


somenewbie

Recommended Posts

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"

Link to comment
https://forums.phpfreaks.com/topic/203568-php-cant-increment-in-recursion/
Share on other sites

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

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);

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.