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 :) )
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With