O'Reilly

Suppressing PHP Errors & Warnings Using @

By on  

One part of making sure your website is secure in its hosting environment is to handle errors in a good fashion. If you don't have complete control of your hosting environment, you don't know what you can expect in the way of error handling if you don't do everything you can think of.

One method I use in all of my programming is to place an '@' character in front of function calls that have potential for returning warnings or errors.

@require($_SITE['ROOT'].$_FOLDERS['CONTROL'].'logger.php'); // suppress a file not found error
@mysql_connect($host,$user,$pass); // suppress no connection error

I recommend using the @ once your website is in production. I generally don't use the @ on development servers because I want to be aware of errors during development.

O'Reilly Velocity Conference
Save 20% with discount code AFF20

Recent Features

  • 6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving,...

  • Page Visibility API

    One event that's always been lacking within the document is a signal for when the user is looking at a given tab, or another tab. When does the user switch off our site to look at something else? When do they come back?...

Incredible Demos

  • MooTools Clipboard Plugin

    The ability to place content into a user's clipboard can be extremely convenient for the user. Instead of clicking and dragging down what could be a lengthy document, the user can copy the contents of a specific area by a single click of a mouse....

  • Create a CSS Flipping Animation

    CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...

Discussion

  1. Quick Update: Doing this is not recommended for large web applications as it can be very expensive.

  2. This is the worst advice I’ve ever heard. This is an old post so I assume (hope) you realise that now, but it ranks very highly on Google, so if you just got here from a search:

    DO NOT DO THIS

    Errors are thrown for a reason, David is correct that displaying them to end-users is incorrect (it’s messy and potentially a security issue), and that errors should be correctly handled. Ignoring is not handling! You can set your php.ini for production and turn off displaying errors, logging them instead, and you can create a custom error handler for more advanced things. Whatever you do, don’t just ignore them!

    • FredWallace

      As he points out, some people may not “have complete control of [their] hosting environment”. That means not everyone has the luxury of editing php.ini.

    • Nemo

      “Worst advice I ever heard” is a very harsh comment. In the right context the @ trick can be very useful.

      Take for instance the getimagesize function that returns size and other information for an image file. The nice thing about it is that it can work with a URL. But what if the URL is no longer valid? It throws up an error. It is far more elegant to simply absorb that error with an @getimagesize and then detect it by checking to see if the length of the “array” returned is less than 7.

      Thank you, David. I have used @ once or twice but I always have to hunt around for it each time I need it.

    • Stephen

      Whilst errors should be handled properly it’s impossible to plan for everything that might go wrong. In that case it would be better to suppress the error on a production site than publicly reveal details about the site’s inner workings.
      As stated by others, people might not have control of their php.ini file but it should be noted that errors can be supressed throughout the rest of the script with:
      ini_set(‘display_errors’, 0);

    • He was writing this from the perspective of people who have no other choice. For example, on a shared hosting environment you’re not likely to have any control over php.ini, so you can’t change those settings. This is, of course, different if dealing with a VPS or dedicated server.

    • zofrex

      There are still other workarounds that are better than this, you can add custom error handlers without needing php.ini access.

      The problem with doing this is it will squash all errors and send them into /dev/null, and your script will keep on running merrily and then probably explode at a later point, making it very difficult to track down where the actual issue is.

  3. Neo

    Cool, never thought about that! This will work great in my CLI scripts ;)

    Thanks

  4. jeff

    Agree with the statement this is very bad advice. Following this will likely leave you hunting bugs and running wild.

    Usually good advice found here. Updating this blog post would be a VERY good idea, since it ranks so high!

  5. Patrick D

    Super helpful, thanks. I don’t necessarily agree with the other comments on here – I think you should just note that this should only be used with great caution and only when absolutely necessary. If there’s another way to approach it, that’s probably preferred, but there are absolutely times when you have to whip this out.

  6. Well, this was useful, when try-catch doesn’t (seem) to work.
    I see lots of comments about avoiding this, but imagine the following…

    You have a webpage, A, that is displaying a PHP image, B. B loads another PHP image, C, and then adds/overlays some graphics ontop. B picks up a random file from a directory and sends information from this file to C. C then reads this information and gets to work on generating an image. Now, if the processing of this file exceeds the Maximum Allowed Memory you can use and you can’t do anything about that, C will return a FATAL ERROR which is recived by imagecreatefrompng, which will return a Warning, and A won’t be able to show the image because it won’t understand a “Warning” image format. Deciding that C can be ignored, but the rest of B is a little more important, I tried to try-catch, but it still displayed the warning. Now I know of @, seems easier… Yeah, I’m lazy, maybe I should wrap that imagecreatefrompng in some custom error handler. That way I might later print the error message onto the B image.

    /Edward

  7. Hari Rao

    Hi David,

    I spent to much time in finding where the problem is but your technique is really awsum. This has helped me to narrow down where the issue is.

    Thanks
    Hari

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Recently on David Walsh Blog

  • Open Files from Command Line on OS X

    I'm as much of a fan of application UIs as anyone else but I'm finding myself working more and more from the command line lately.  Much of that is becoming obsessed with media manipulation but I'm forcing myself to use less UIs so that I...

  • Get Stock Quotes From Command Line

    When I conned my way into my first professional programming gig, I didn't really think much about money -- just that I was getting my foot in the door.  But as my career has gone on, I've been more aware of money, investing, and retirement.  I've recently...

  • Geolocation API

    One interesting aspect of web development is geolocation; where is your user viewing your website from? You can base your language locale on that data or show certain products in your store based on the user's location. Let's examine how you can...

  • Create an Image Preview from a Video

    Visuals are everything when it comes to media.  When I'm trying to decide whether to watch a video on Netflix, it would be awesome to see a trailer of some kind, but alas that isn't available.  When I'm looking to download a video on my computer,...

  • New:  Webdesigner News!

    A new and exciting website has recently been launched for web designers and developers. You likely spend hours every morning browsing through hundreds of posts on your RSS feeds, hoping to stumble across relevant stories. Webdesigner News was built to provide web designers and developers with...