Suppress Error With @ Operator in PHP

Suppress error with @ operator in PHP

I would suppress the error and handle it. Otherwise you may have a TOCTOU issue (Time-of-check, time-of-use. For example a file may get deleted after file_exists returns true, but before fopen).

But I wouldn't just suppress errors to make them go away. These better be visible.

How do I disable error suppression @ operator in PHP?

OK, This is a life saver, If you have xdebug installed there is a flag which does exactly that, it suppresses the error suppressor :) Effectively making it easier to debug this hell of a code.

xdebug.scream=1

@ error suppression operator and set_error_handler

The @ operator temporarily sets error_reporting to 0, so you can test the value of error_reporting in your error handler:

if (ini_get('error_reporting') == 0) {
return;
}

Or even better, log only error types that are in error_reporting:

$error_reporting = ini_get('error_reporting');

if ( !($error_reporting & $errno) ) {
return;
}

Also take a look at the log_errors and error_log options, for automatically logging errors to a file or to syslog.

When to suppress errors in PHP

In my experience, you should never suppress errors on an individual level. In your production environment set error reporting to 0 and display errors to off. Having individual supressions like this is shooting yourself in the foot when you come back to fix an error in 6 months, forget it's there and can't work out why no errors are showing up but something is clearly broken.

In you particular case, just set $isLoggedIn to false then override that value at the location you are currently instantiating it (I'm assuming code structure here).

PHP - when to use error suppressing operator ( @( $obj-var ) ) and when to use null coalescing operator ( ?? )

The @ operator is sometimes called the "shut up" operator: it silences any and all warnings and errors produced by the given statement. That makes it a rather crude tool: rather than saying "I am aware of a specific case in this code which I need to handle", it says "whatever goes wrong, just carry on and don't tell me about it".

In contrast, the ?? operator (and the newer ?-> operator) handle one specific case: the left-hand side might be null or unset, and you want to define the behaviour if it is.

The effect of the two expressions is probably quite similar in this case, but the intent with the ?? operator is much clearer.

Even better is to write code where you don't need such cases at all - for instance, a private property that is only written in the constructor is always in a known state; a public method returning that value can then return it without extra error suppression.

So instead of:

class MyClass {
public $var_value;
}
$my_obj = new MyClass;

// I might have forgotten to set the property, so coalesce it
echo $my_obj->var_value ?? '';

You can write:

class MyClass {
private string $var_value;
public function __construct(string $var_value) {
$this->var_value = $var_value;
}
public function get_var_value() {
return $this->var_value;
}
}
$my_obj = new MyClass('hello');

// No way for this to error, so no extra handling needed
echo $my_obj->get_var_value();

@ doesn't suppress errors in php

It turns out I was using a separate error handler. I added the following check to my error handler:

if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return;
}

and now it correctly skips suppressed errors.

source: http://us2.php.net/manual/en/function.set-error-handler.php

PHP: Disable @ error control operator

Yes you can, there's a library called scream, http://php.net/manual/en/book.scream.php once installed you can do:

ini_set('scream.enabled', true);

Or directly in php.ini

scream.enabled = On


Related Topics



Leave a reply



Submit