naran.arethiya
naran.arethiya

Reputation: 43

php function return blank value on return statement and print right on echo

i create this function for search resume file from directory, if resume is available then function return full path, problem is function return nothing if i use "return", if i use "echo" then it will print right path

 function search_resume($resume,$dir="uploads/resumes")
{
    $root = scandir($dir);
    foreach($root as $value)
    {
        /* echo $value."<br/>"; */
        if($value === '.' || $value === '..') {continue;} 
        if(is_file("$dir/$value"))
        {
            if($value==$resume)
            {
                $path="$dir/$value";
                return $path;
            }
        }
        else
        {
            search_resume($resume,"$dir/$value");
        }
    }
}

Upvotes: 1

Views: 555

Answers (1)

deceze
deceze

Reputation: 522085

A very typical, basic problem with recursive functions: you need to return recursive calls as well, they're not going to return themselves.

    ...
    else {
        $path = search_resume($resume,"$dir/$value");
        if ($path) {
            return $path;
        }
    }

Upvotes: 1

Related Questions