print_r is your friend?

Submitted by nk on Wed, 2008-03-26 06:05.

Someone said this in #drupal today. I long wanted to blog about this. I never use print_r. There is no need for it, var_export is far superior IMO -- if I need to write a quick script which actually does something on the data, I can copypaste the output into PHP code. Little but annoying things like escaping apostrophes are handled too, so it's even useful for dumping strings. Of course, var_dump is also useful as that dumps type info -- how cool it would be if it would do that in a PHP comment and then it would also render valid PHP code as debug output.

Submitted by theborg on Wed, 2008-03-26 13:55.

Agree totally,

I use var_export with TRUE as the second paramenter so the output can be redirected to drupal message function:

drupal_set_message('< pre >'. var_export(array_to_dump,TRUE) .'< /pre >');

You can define this as a macro in your favorite editor to quick & dirty debug arrays or variables.

User login

Log in using OpenID

No user registration here. Use your DrupalID from drupal.org, for eg. chx@drupal.org