“@”是什么意思? PHP 中的前缀做什么?

发布于 2024-10-11 05:12:47 字数 74 浏览 6 评论 0原文

下面代码中的“@”符号有什么作用?

@mkdir(ROOT. "cache/");

What does the '@' symbol do in the following code?

@mkdir(ROOT. "cache/");

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

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

发布评论

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

评论(3

猛虎独行 2024-10-18 05:12:47

它禁止显示错误

PHP 支持一种错误控制运算符:at 符号 (@)。当添加到 PHP 中的表达式之前时,该表达式可能生成的任何错误消息都将被忽略。

如果启用了track_errors功能,则表达式生成的任何错误消息都将保存在变量$php_errormsg中。此变量将在每次错误时被覆盖,因此如果您想使用它,请尽早检查。

正如评论中所指出的,我也无法想象实际使用此功能的原因——编写适当处理错误状态/条件的代码。

It suppresses errors from displaying:

PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.

If the track_errors feature is enabled, any error message generated by the expression will be saved in the variable $php_errormsg. This variable will be overwritten on each error, so check early if you want to use it.

As noted in the comments, I too cannot imagine a reason to actually use this functionality -- write code that deals appropriately with error states/conditions.

行至春深 2024-10-18 05:12:47

正如所指出的,它是错误抑制算子。

但没有指出的是,使用这种做法是非常糟糕的——错误不应该默默地失败。

检查错误返回,并在使用异常的地方使用 try/catch 块。

在具体示例中...

@mkdir(ROOT. "cache/");

...它会忽略来自 mkdir() 的任何错误。 docs 说它在失败时返回 FALSE ,所以你应该这样做。 ..

if ( ! mkdir(ROOT. "cache/")) {
   // Handle error.
}

As pointed out, it is the error suppression operator.

But what has not been pointed out, is that it is very bad practice to use - errors should not fail silently.

Check for error returns, and use try/catch blocks where exceptions are being used.

In the specific example...

@mkdir(ROOT. "cache/");

...it ignores any errors from mkdir(). The docs says it returns FALSE on failure, so you should be doing...

if ( ! mkdir(ROOT. "cache/")) {
   // Handle error.
}
如此安好 2024-10-18 05:12:47

人们似乎忘记了 PHP 是一种快速完成任务的肮脏语言,直到最近它才试图变得成熟和复杂。

错误抑制是一种让函数按照您需要的方式运行的快速而肮脏的方法,因为在 Web 开发中您无法预测将会向您抛出什么,有时并不值得关心!

一个典型的例子是有用的函数 getimagesize,它允许您获取有关某人上传的图像的一些信息。
如果图像文件不是标准图像文件,则此函数会抛出一个抖动。检查文件、确定是否可以将其加载到 getimagesize 中并不是真正的开发人员角色。可能有一些优雅的方法可以做到这一点,但说真的,我不在乎!

只需这样做:

if( !($a = @getimagesize(  $_FILE['file']['tmp_name'] )))
{
   unlink( $_FILE['file']['tmp_name'] );

   //politely tell user that you rejected their image!
}

是的,您可以使用更优雅的 try 和 catch 语句,但最终,您捕获了错误并抑制了错误消息,这就是您想要的,而又不会磨损 tab 键!

与上面的答案相反,仔细使用@前缀不会导致失控的火车失事。它只是允许开发人员以他们喜欢的方式容纳错误。

People seem to forget that PHP was a quick dirty language for getting things done, only recently has it tried to be mature and sophisticated.

Error suppression is a quick and dirty way of making functions behave the way you need them to, because in web-development you cannot predict what will be thrown at you, and sometimes it is not worth caring!

A classic example is the useful function getimagesize, that allows you to get some information about an image that someone has uploaded.
This function chucks a wobbly if the image file is not a standard image file. It is not really the developers role to inspect a file, determine if it can be loaded into getimagesize. There might be elegant ways of doing this, but seriously I don't care!

just do this:

if( !($a = @getimagesize(  $_FILE['file']['tmp_name'] )))
{
   unlink( $_FILE['file']['tmp_name'] );

   //politely tell user that you rejected their image!
}

yes, you could use try and catch statements which are more elegant, but in the end, you have caught the error and suppressed the error message, which is what you wanted without wearing out the tab-key!

Contrary to what above answers say, the @ prefix used carefully does not result in a runaway train wreck. It just allows the developer to accommodate errors in the way they prefer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文