如果“file_exists”是错误的被设计为返回路径而不是 TRUE?

发布于 2024-10-21 12:32:48 字数 423 浏览 2 评论 0原文

file_exists 函数成功时返回 TRUE,但我认为如果它返回值而不只是 TRUE,会更有用传递的$filename。我这样想有错吗?

如果函数是这样设计的,我们就可以使用:

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

if($file)
{
    // do something
}

... 而不是更复杂的:

$file = "dir/file.ext";

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

if($success)
{
    // do something
}

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

离去的眼神 2024-10-28 12:32:48

我不明白为什么这会是一个改进。请考虑:

  1. 实际上,您会将 file_exists 调用放在条件内,因此不会有额外的 $success 变量
  2. 名称 file_exists 预先决定了代码的读者认为该函数将返回是/否答案(布尔值)

所以总之,我认为除了返回值的类型之外什么都不改变是一个坏主意。

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.

孤星 2024-10-28 12:32:48

文件存在吗?基本上是一个是或否问题,因此它给出是或否(布尔)答案。

该文件存在吗? “是”,它存在。

所以它的名字恰如其分

file_exists? is basically a yes or no question, so it gives a yes or no (boolean) answer.

Does the file exist? "Yes", it exists.

So it's aptly named for what it does

握住你手 2024-10-28 12:32:48

那毫无意义。它还可以返回文件权限、文件大小、最后修改日期,而且它仍然与文件名一样有意义。它只不过是一个给出布尔值的检查,你可以根据它做出决定。

顺便说一句,我没有得到你的例子,有什么比以下更容易的:

$file = "dir/file.ext";
if(file_exists($file))
{
    // do something with $file
}

或者如果你以后确实需要返回值,你可以这样做:

$file = "dir/file.ext";
if($success=file_exists($file))
{
    // do something with $file
}

That would just make no sense. It could also return the file permissions, the file size, the last modified date, and still it would make as much sense as the filename. It is nothing but a check that gives a Boolean value, and you can make decisions based on it.

By the way, I don't get your examples, what could be easier than:

$file = "dir/file.ext";
if(file_exists($file))
{
    // do something with $file
}

Or if you really need the return value later, you could do:

$file = "dir/file.ext";
if($success=file_exists($file))
{
    // do something with $file
}
烈酒灼喉 2024-10-28 12:32:48

好吧,没有什么能真正阻止您编写这个函数:

function get_filename_if_exists($fname) {
    return (file_exists($fname) ? $fname : FALSE );
}

恕我直言,它是有问题的,因为它有多种返回类型,并且不会为您提供比从 file_exists() 获得的更多信息 - 因此比毫无意义更糟糕;但如果你真的愿意的话,也可以这样搬起石头砸自己的脚。

(由于此处和其他答案中所述的原因,这种行为不太可能被改进到 file_exists() 中)

Well, nothing really prevents you from writing this function:

function get_filename_if_exists($fname) {
    return (file_exists($fname) ? $fname : FALSE );
}

It is IMHO problematic as it has multiple return types, and doesn't give you any more information than you'd get from file_exists() - and therefore worse than pointless; but it is possible to shoot yourself in the foot like this, if you really want.

(this behavior is unlikely to get retrofitted into file_exists(), for the reasons stated here and in the other answers)

灼痛 2024-10-28 12:32:48

那么fopen有什么用呢?

$res = fopen("my/path.txt");
if ($res===FALSE) {
  // File does not exists or an error while opening
}

如果您特别想知道文件是否存在,那么 file_exists 函数会执行它应该执行的操作。

if (FALSE===file_exists("my/path.txt")) {
  // Stop here, 
}

Then what would be the use of fopen?

$res = fopen("my/path.txt");
if ($res===FALSE) {
  // File does not exists or an error while opening
}

If you specifically want to know whether a file exists then the file_exists function does what it is supposed to do.

if (FALSE===file_exists("my/path.txt")) {
  // Stop here, 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文