• 13

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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

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.

  • 41
Reply Report
    • 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.
      • 2
    • +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.
    • 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.

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.
  • 8
Reply Report

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.

  • 5
Reply Report
    • With PHP notices on, I think the @ suppressor becomes very useful, ie: if( @$_POST['submit'] == 'send' ){}
      • 1
    • 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.
      • 2
    • 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.

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions