How to debug in Codeigniter

Date : 09/11/2019

Introduction

We can use functions like echo, print_r(), var_dump() or log_message() to print values both in log files and also in browser. Other than log_message() other print methods will not print values in log_message.

echo – used to simply display the content.

print_r() – used to display value in array format.

var_dump() – same like print_r(). But in slightly different and clear way such datatype of every values and size of array if the variable passed to var_dump() has any array.

log_message() – used to print messages only in log files.

Set-up for log_message()

You need to set your $config[‘log_threshold’] in file config.php since by default it’s 0 (no logs)

  • 0 = Disables logging, Error logging TURNED OFF
  • 1 = Error Messages (including PHP errors)
  • 2 = Debug Messages
  • 3 = Informational Messages
  • 4 = All Messages

If you have set level to 2, below example print the output in the log file you have mention.

log_message(‘debug’, print_r(‘debugging enabled‘));

=> DEBUG - 2019-11-08 15:41:48 --> debugging enabled

Set-Up for var_dump() :

We can use echo or var_dump() in yourcontroller.php or yourmodel.php file to display it the browser. It can be helpful to check the outputs of those files in the browser itself.

For example, You are sending request from front-end with ajax. Whether the call is success or not, you can see successful var_dump() operations from your controller and model file in the browser itself. To see those successful outputs, do something like the follow.

$.ajax({

url : ‘your_url’,
data : ‘id= xxx’,
type : ‘post’,

success : function(response){

$(‘html’).html(response);

},

error : function(error){

$(‘html’).html(error);

}

});

If you used echo or var_dump() in Controller or Model file, the above code will display all the echo and var_dumps() values of controller and model file’s var_dump() and echo outputs in the browser. By doing this, you can verify every operation give your output or anything went wrong quickly.

Conclusion

Thank you for using pheonixsolutions. If you find it useful, share it with your friends.

Leave a Reply