Search code examples
phplanguage-design

Would it be wrong if "file_exists" was designed to return a path instead of TRUE?


The file_exists function returns TRUE on success but I think it would be more useful if instead of just TRUE, it returned the value of the passed $filename. Am I wrong to think that?

If the function was designed like that, we would be able to use:

$file = file_exists("dir/file.ext");

if($file)
{
    // do something
}

... instead of the more complicated:

$file = "dir/file.ext";

$success = file_exists("dir/file.ext");

if($success)
{
    // do something
}

Solution

  • I don't see why that would be an improvement. Consider that:

    1. In practice you will put the file_exists call inside the condition, thus there will be no extra $success variable
    2. The name file_exists predisposes the reader of the code that the function will return a yes/no answer (boolean value)

    So in conclusion, I think that it would be a bad idea to change nothing but the type of the return value.