What does the '@' symbol do in the following code?
@mkdir(ROOT. "cache/");
What does the '@' symbol do in the following code?
@mkdir(ROOT. "cache/");
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.
error_reporting(0);
for two reasons: A. it's quicker, B. it's easier to change later. –
Undies error_reporting()
twice. It's often better to disable errors before the loop and then reset the error level after the loop is done. –
Microscopium ErrorException
). Instead of using try/catch you would use the @
and check the return value for an error case. Of course, if it's all in the same codebase, you would use only one kind of error handling, but it can be the only way possible when several libraries are involved. –
Cloud fopen($file, 'x')
which produces an E_WARNING error if a file of that name already exists. –
Candless 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.
}
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.
© 2022 - 2024 — McMap. All rights reserved.