What does the '@' prefix do in PHP? [duplicate]
Asked Answered
H

3

38

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

@mkdir(ROOT. "cache/");
Hex answered 11/1, 2011 at 1:33 Comment(3)
Sure. Try searching PHP site for "@" or "@ prefix" - gets you a long way. NOT.Hex
"This operator is affectionately known by veteran phpers as the stfu operator." ;-)Oceanid
@Rich Turner - searching the current PHP website for @ and @ prefix yields nothing :-(Catamount
D
42

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.

Drucilla answered 11/1, 2011 at 1:34 Comment(10)
Thanks for the answer. This just makes the disgusting code I've inherited even worse than I thought it was!Hex
Because it will be most difficult to debug, try to avoid this feature, especially on critical code.Toscana
If you plan on suppressing errors, you're better off using error_reporting(0); for two reasons: A. it's quicker, B. it's easier to change later.Undies
+1 Just like other languages (per example, C), a warning or notice is thrown at you for a reason. They should not be ignored, but dealt with accordingly.Shampoo
@John: Funny because E_NONE is not actually a defined constant and will actually spit a E_NOTICE error!Shampoo
Be careful when using @ in a loop: Each run will trigger 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
@netcoder: I realized that. That's why I corrected myself to 0.Undies
It can be useful in isolated cases when you also set an error_handler that throws exceptions (e.g. one that converts error messages into 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
One instance where error suppression with @ could be useful is in the case where you're looking for a unique file name with fopen($file, 'x') which produces an E_WARNING error if a file of that name already exists.Candless
I used @ numerous times in my PHP5 code and it worked well. Just upgraded to PHP8 and now the @ is ignored and all the errors are appearing in all their glory ! Now i have to fix all that code by adding proper error handling.Catamount
M
8

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.
}
Merited answered 11/1, 2011 at 1:39 Comment(0)
M
7

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.

Mulloy answered 27/2, 2016 at 11:41 Comment(6)
With PHP notices on, I think the @ suppressor becomes very useful, ie: if( @$_POST['submit'] == 'send' ){}Mulloy
"There might be elegant ways of doing this, but seriously I don't care!" I hope I'll never touch your code.Walls
I really like this answer. I'm quite new to PHP and its quite easy to accidentally generate errors you don't want users to see. This operator seems to be an easy fix.Steerageway
"its quite easy to accidentally generate errors you don't want users to see"Mulloy
Sorry, trying to say "its quite easy to accidentally generate errors you don't want users to see" is not the reason to use @. You still need to take responsibility for the condition.Mulloy
OK - I am being hypocritical, yes it does suppress the warning, but only use it when you know what you are doing!Mulloy

© 2022 - 2024 — McMap. All rights reserved.