Print Variables to Screen in Drupal

One of the most useful things I discovered in Drupal was how to print variables to the screen. I use it in almost every project I work on, so I thought I'd share this little snippet. All you have to do is print your variable using Drupal's the drupal_set_message function to send it to the screen. And by wrapping it in a sprint_f, print_r, and pre tag we can safely output it in a nice format.

drupal_set_message(sprintf('<pre>%s</pre>', print_r($variable, TRUE)));

I'm sure there are other ways to do this, but this is a nice quick and dirty way to see what you're working with.

Tags: