print_r shortcut in codeigniter

In codeigniter or php, we frequently use these lines for debugging:

echo “<pre>”;
print_r($myArray);
echo “</pre>”;
die;

To avoid these lines to write again and again, I have defined a function in my comman_helper (application/helpers/comman_helper.php):

<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
function pr($myArray = array(), $terminate = true) {
echo “<pre>”;
print_r($myArray);
if($terminate) {
die;
}
echo “</pre>”;
}
?>

Now wherever you want to print array in your code, you can import helper and print array as below:

<?php
defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
class User extends My_Controller {
public function index() {
$this->load->helper(‘comman_helper’);
$ar = array(1,2,4);
pr($ar);
$this->load->view(‘welcome_message’);
}
}
?>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.