Home » Php » What does the '@' prefix do in PHP?

What does the '@' prefix do in PHP?

Posted by: admin April 23, 2020 Leave a comment


What does the ‘@’ symbol do in the following code?

@mkdir(ROOT. "cache/");
How to&Answers:

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.


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.