It's a good idea to use pr()…

CakePHP’s built-in wrapper for print_r(), pr() is a nice little function, which is best used for quick debug output.

Reason being, is that if you accidentally forget to remove one call here or there it won’t produce any output if you are in debug = 0 mode (AKA production).

Here it is in all its glory from basics.php:

function pr($var) {
  if (Configure::read() > 0) {
      echo '<pre>';
  print_r($var);
      echo '</pre>';
  }
}

There is also a debug() function, which is slightly more robust, but works in a similar fashion.

P.S. Do take a look at basics.php in the core, as it has a couple of other nice utility functions like h(), for example.

… and thanks to AxlF for pointing everyone in the right direction in the manual: http://book.cakephp.org/view/121/Global-Functions

m4s0n501
  • AxlF

    Hi there,

    one tip i read in one of the blogs out there (i think it was felix geisendörfers – debuggable):

    I often used this:
    pr($data)
    die();

    so I created a function for that in bootstrap:

    function prd($var) {
    pr($var);
    die();
    }

    Regards,
    AxlF

  • http://www.mikebernat.com Mike Bernat

    I’m all about using var_dump() in tandem with Xdebug. It color codes and formats the output for extremely easy viewing. The advantage to using pr() is that it checks the debug value before spitting out a lot of, possibly dangerous, information. This functionality could easily be incorporated with some kind of var_dump() wrapper. vd() perhaps?

    Just a thought :p

  • http://teknoid.wordpress.com teknoid

    @AxlF

    Yep, nice little improvement indeed…

    @Mike Bernat

    I think that’s a pretty good idea, as one of the reasons that prompted me to post this was the fact that a few people use var_dump() (or print_r() directly), which as you’ve pointed out, has an unfortunate drawback for us, forgetfull developers. As a matter of fact I do like using it with xdebug as well, and everyone loves orange… hehe ;)

    A little patch to basics.php to add vd() might be in order…

  • AxlF

    Another addition – It’s already in the book:

    http://book.cakephp.org/view/121/Global-Functions

  • http://teknoid.wordpress.com teknoid

    @AxlF

    Ah, great addition… I’ve updated the post with the link.

  • http://blog.sohaibmuneer.com SayB

    You can also write another custom function and name it “prd()” – it simply does a pr() and die; The idea was originally implemented by Tim from http://debuggable.com and I’ve found it to be very very useful when you want to pr an array and stop execution

  • http://teknoid.wordpress.com teknoid

    @SayB

    Yep, and AxlF managed to sneak in a similar comment slightly ahead :)

    Now, without stirring too much trouble… I will always say that using die(); in OOP is a bad practice… unfortunately it is inherent in PHP, for now.

  • http://bjornskov.eu Henrik

    Imo should the function be
    function pr($var) {
    if (Configure::read() > 0) {
    echo ” . print_r($var, true) . ”;
    }
    }

  • Pingback: It’s a good idea to use pr()… | Dailytuts.net - Daily tutorial for peoples()

  • http://www.masonqm.com/ Chris Fulton

    function prv($var) {
    if (Configure::read(‘debug’) > 0) {
    echo ”;
    $textout = print_r($var, true);
    print htmlentities($textout);
    echo ”;
    }
    }

  • Fakhrul

    The boolean FALSE and NULL can not be seen using print/echo or the print_r

    var_dump is the solution.

    Most probably Cakephp does not have a wrapper of this.

  • teknoid

    @Fakhrul

    No, but you can easily create one in bootstrap.php (you can search other posts for example)