In this tutorial we are going to learn CodeIgniter Error Handling. Many times, we encounter errors when using application. It’s very annoying to users if the errors are not properly handled. CodeIgniter provides an easy mechanism for handling errors.

You want to display the messages when the application is in development mode instead of in production mode because the error messages can be easily solved at the development stage.

You can change the environment of your application by changing the line in the index.php file. This can be set to anything, but usually, there are three values used for this purpose (development, testing, production).

define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');

Different settings require different levels of reporting errors. Development mode defaults to display errors and tests, and live mode hides them. CodeIgniter provides three functions for handling errors as shown below.


The show_error() function displays errors to the top of the screen in HTML format.


show_error($message, $status_code, $heading = 'An Error Was Encountered')


  • $message (mixed) ? Error message
  • $status_code (int) ? HTTP Response status code
  • $heading (string) ? Error page heading


The show_404() function displays an error when you try to access a non-existent page.


show_404($page = '', $log_error = TRUE)


  • $page (string) – URI string
  • $log_error (bool) – Whether to log the error


For writing log messages, the log_message() function is used.


log_message($level, $message, $php_error = FALSE)


  • $level (string) ? Log level: ‘error’, ‘debug’ or ‘info’
  • $message (string) ? Message to log
  • $php_error (bool) ? Whether we’re logging a native PHP error message

You can enable logging under application/config/config.php file.

|   Error Logging Threshold
| You can enable error logging by setting a threshold over zero. The 
| threshold determines what gets logged. Threshold options are:
|   0 = Disable logging, Error logging TURNED OFF
|   1 = Error Message (including PHP errors)
|   2 = Debug Message
|   3 = Informational Messages
|   4 = All Messages
| You can also pass an array with threshold levels to show individual error types
|   array(2) = Debug Message, without Error Messages
| For a live site you'll usually only enable Errors (1) to be logged otherwise 
| your log files will fill up very fast.
$config['log_threshold'] = 1;

The log messages can be found in application/log/. Make sure this directory is writable before log files are enabled. Several error message templates are available in application/views/errors/cli or application/views/errors/html.