开发者

PHP function not returning string [duplicate]

This question already has answers here: 开发者_开发技巧 How to use return inside a recursive function in PHP (4 answers) Closed 7 months ago.

I have constructed a function that takes a filename, and increments a counter in the filename and returns it, however, everything is correct, except the return does not return the filename.

Any help, please?

My code:

$filename = join("", array_reverse($date));
$filename .= ".xml";
$dir = "../gigs";
$file = $dir."/".$filename;

function getNewFileName($filename, $dir) {
if (is_file("$dir/$filename")) {
    if (strpos($filename, "_") === false) {
        $filename = str_replace(".xml","_1.xml",$filename);
        getNewFileName($filename, $dir);
    }
    else {
            $pos = strpos($filename, "_");
            $counter = (int)substr($filename, $pos+1,1);
            $counter++;
            $filename = substr($filename,0, $pos)."_".$counter.".xml";
            getNewFileName($filename, $dir);
        }
    } else {
                // echoing HERE shows that the string is manipulated correctly
        return (string)$filename; // but returning here is not working
    }
}

echo getNewFileName($filename, $dir); // <- this last line prints nothing out

Thanks in advance.


The line:

getNewFileName($filename, $dir);

needs a return:

return getNewFileName($filename, $dir);


This is what your function should look like:

function getNewFileName($filename, $dir) {
   if (is_file("$dir/$filename")) {
       if (strpos($filename, "_") === false) {
           $filename = str_replace(".xml","_1.xml",$filename);
           return getNewFileName($filename, $dir);
       }
       else {
               $pos = strpos($filename, "_");
               $counter = (int)substr($filename, $pos+1,1);
               $counter++;
               $filename = substr($filename,0, $pos)."_".$counter.".xml";
               return getNewFileName($filename, $dir);
       }
    }
    return (string)$filename;
}

echo getNewFileName($filename, $dir); // <- this last line prints nothing out


Firstly, please try and format your code so the indents are readable. Second, you're just not returning from recursive calls to getNewFileName():

function getNewFileName($filename, $dir) {
  if (is_file("$dir/$filename")) {
    if (strpos($filename, "_") === false) {
      $filename = str_replace(".xml","_1.xml",$filename);
      return getNewFileName($filename, $dir); // here
    } else {
      $pos = strpos($filename, "_");
      $counter = (int)substr($filename, $pos+1,1);
      $counter++;
      $filename = substr($filename,0, $pos)."_".$counter.".xml";
      return getNewFileName($filename, $dir); // and here
    }
  } else {
    return (string)$filename;
  }
}

assuming that's your intent.


function getNewFileName($filename, $dir) {
  if (is_file("$dir/$filename")) {
    if (strpos($filename, "_") === false) {
      $filename = str_replace(".xml","_1.xml",$filename);
      return (string)$filename;
    } else {
      $pos = strpos($filename, "_");
      $counter = (int)substr($filename, $pos+1,1);
      $counter++;
      $filename = substr($filename,0, $pos)."_".$counter.".xml";
      return (string)$filename;
    }
  } else {
    return (string)$filename;
  }
}

Your function had a loop to infinity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜