PHP: Improving print_r() with print_pre() Wrapper Function

When you need a simple debugging function and print_r() is just not enough, you need a good wrapper function. You need print_pre().

Most print_pre() functions out there just wrap the print_r() output with the <pre> tag. Useful, but not that useful. This print_pre() function has evolved over the time I have used it. I have used it across multiple jobs. Where it is now is good, anything added to it would be for specialized cases.

This print_pre() function does a bit more than just wrap the print_r() with <pre> tags. Sometimes you may not be able to view any output easily, so it may output to a file if you so desire. It can be used in place of any print_r() as it may be used exactly the same. Another cool feature is word wrapping, which basically does away with the <pre> tag and wraps it in a <p><tt> combo.

function print_pre ($expression, $return = false, $wrap = false)
  $css = 'border:1px dashed #06f;background:#69f;padding:1em;text-align:left;';
  if ($wrap) {
    $str = '<p style="' . $css . '"><tt>' . str_replace(
      array('  ', "\n"), array('&nbsp; ', '<br />'),
      htmlspecialchars(print_r($expression, true))
    ) . '</tt></p>';
  } else {
    $str = '<pre style="' . $css . '">'
    . htmlspecialchars(print_r($expression, true)) . '</pre>';
  if ($return) {
    if (is_string($return) && $fh = fopen($return, 'a')) {
      fwrite($fh, $str);
    return $str;
  } else
    echo $str;

The print_pre() function has simple usage:

mixed print_pre (mixed $expression [, mixed $return = false [, bool $wrap = false ]] )

The second parameter, return, works just like the return parameter on print_r() except when a non-empty string is passed. If a string is passed to the return parameter, it will be treated as a file path to save the output to and will continue to return the output. The last parameter, wrap, is rather self explanatory; it will allow long lines to break and the text will not be preformatted if set to TRUE.

Everybody likes examples, so here are a few for you:

// Let us assume print_pre() and $obj were defined before this point

// the output is simply printed

$string = print_pre($obj,true);
// $string contains the output

echo print_pre($obj,'debug.htm',true);
// the output allows wrapping, is saved to debug.htm and then printed

And that is the print_pre() function. Ok, my print_pre() function. Enjoy!

Suspended Animation

I just watched this TED Talk from last year where biochemist Mark Roth explained the advancements that were made in the field of suspended animation.

He talked about how suspended animation with the right concentrations and exposure of the toxic gas hydrogen sulfide has helped reduce damage to organs and tissue. Mark also mentioned how much this can help people in trauma.

This has got me thinking about the possibilities this may unlock. If there can be a standard way to suspend a trauma victim and buy them a tremendous amount of time, this could be one of the best advancements in modern medicine. Just think about small town out in the middle of nowhere, where there is no hospital for miles, that has this on hand. If someone in the town has a heart attack and is administered hydrogen sulfide to give them more time, they could be airlifted to the nearest hospital suspended, but still alive, where they can then get all the medical attention they need. This could give proper medical attention to those who may otherwise have no choice but to be treated by someone who is medically untrained and is subject to possibly fatal mistakes.

Much is yet to be learned about suspended animation, but if we can get through it, perfect it, and establish it safely into society, I can’t imagine how many lives it could save.

What are your thoughts on suspended animation? What do you believe the future could be like with suspended animation?

The Adventure

Ok. Well, the first post of my hopefully active blog is this.

I’m really looking forward to getting back in action and start blogging once more. My previous blog over at has been abandoned, at least for now.

So, what am I planning for this blog? I haven’t planned to be honest. My good friend, Erin Mckay, is an avid blogger and told me that I should just make my first post. Now that I have done that, I must think about what I need to do next. I do have some ideas for future posts, some are just going to be updated versions of posts I made on my old blog.

I’m sure I’m going to have to search deep within myself to get to the good stuff. Going that deep inside myself sounds scary to me. Inside, I have a dark side and a light side just the same way as everyone else. Expressing myself is something I always had a challenge doing, so I think it may take some time to see more personal posts emerging from the cracks.

Another adventure I seek is WordPress itself. Any regular to the internet may recognize that this blog is WordPress. This is my first real use of WordPress and I am looking forward to working a lot with it and maybe even build a plugin or two. My reasoning is simple; I want to learn. Well, that and the fact that WordPress has been touted as the best CMS, or content management system, available. I previously used Drupal, which my previous blog is using. My experience with Drupal has been okay, but not the smoothest of rides as every new major version seems to never fail to break something. As Drupal is a nice CMS, I figured I should give WordPress a shot, probably something I should have done a long, long time ago. Another good reason to learn WordPress is for a job. I have been noticing in my recent job searching that WordPress is mentioned more than any other CMS, so the choice from that alone seems to be obvious.

So, I think I should save all the rest of my rambling for other posts.

I would like to invite you on an adventure. The adventure is here. The adventure is now. The adventure is abnormal. The adventure is me. Welcome and mind your step.