Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I suppress a notice error in codeigniter?

I want to suppress notices in the codeigniter error log during a certain cronjob. I've tried using the @ in front of the line, but it still prints the notice in the log:

This is the row that is generating the notice:

@$resultarray[1][2]++;

The notice: ... Severity: Notice --> Undefined offset: 1 ...

What am I doing wrong here?

(I'm using it well overthought, so please no messages claiming not to use @, thanks :) )

like image 495
Pim Avatar asked Dec 25 '22 10:12

Pim


1 Answers

To have PHP report all errors except E_Notice, call the PHP error_reporting function like so:

error_reporting(E_ALL & ~E_NOTICE);

I believe the best way to do this is to configure your index.php at the root of the application to first detect your application environment and then conditionally set which PHP errors are reported.

index.php

if (defined('ENVIRONMENT'))
{
  switch (ENVIRONMENT)
  {
    case 'development':
    case 'testing':
    case 'staging':
      error_reporting(E_ALL & ~E_NOTICE);
    break;

    case 'production':
      error_reporting(0);
    break;

    default:
      exit('The application environment is not set correctly.');
  }
}

See the official PHP Documentation about the error_reporting function for more configuration options.

Alternatively, you can just call the error_reporting function in the particular file you're using (whatever your cron job calls) and it'll overwrite the configuration with what you set in index.php. Here's a simple example using a controller:

class Sample_Controller extends CI_Controller {

  public function __construct()
  {
    parent::__construct();
    error_reporting(E_ALL & ~E_NOTICE);
  }

}
like image 175
grant Avatar answered Feb 16 '23 00:02

grant