recursion - return keyword is not working correctly in php -
i using piece of function check if file exist. if file exist changing name
function checkfileexist($filename,$z,$ext,$folderwithfilename){ $tmpnam=""; if (file_exists($filename."_".$z.".".$ext)) { $z=$z+1; checkfileexist($filename,$z,$ext,$folderwithfilename); }else{ $tmpnam=$filename."_".$z; echo "in else <br> ".$filename."_".$z."<br>"; } return $tmpnam; }
and calling function like
$definename=checkfileexist($folderwithfilename."/".$invid,$z,$ext,$folderwithfilename); echo "new name ".$definename;
but gives me output this
in else 444_2015-10-27/444_3 new name
you can see return not working correctly, doing wrong here?
you should use return checkfileexist($filename,$z,$ext,$folderwithfilename);
in first block return value recursively made calls. use clean version:
function checkfileexist($filename, $z, $ext, $folderwithfilename){ if (file_exists($filename."_".$z.".".$ext)) { return checkfileexist($filename, $z+1, $ext, $folderwithfilename); } return $filename."_".$z; }
Comments
Post a Comment