I'm working on some code for a custom Drupal 7 module the problem is i'm dealing with many arrays and strings. I have tried to use print(), and print_r() to output values so i can see what i'm working with but nothing is printed to the screen. Is Drupal 7 preventing this from happening? How do other Drupal 7 module developers output values? for example in JavaScript i can use console.log() and for PHP i would normally use print(), print_r(), and echo. But looks like Drupal is not permitting this from working in my PHP code.


In my module i have .inc file inside this file i have a method called list_builder($delta){} Inside this i have a switch() to handle different lists based on the $delta. This method is being executed on the cron_hook. It processes data from some outside sources and builds a json file. I'm at the point of building my json output but dealing with a lot of variables and need to be able to look at the variables at each spot in the process to know what data i'm dealing with while i build the output. I'm looking for a way to diagnostically test/display variables as i work on this module. I'm looking for a universal solution that works no mater what i'm doing.

  • 1
    It really depends on where your code is as to which debugging methods will work. – rooby Jan 20 '16 at 0:02
  • Please don't make it a chameleon. – Mołot Jan 20 '16 at 16:06

It's the same as in other PHP applications with separate V layer - print and print_r, being directly PHP functions, don't know about anything in View layer of Drupal application. They cannot pass their output to theming chain, cache systems in place and so on. That said, they often works, but that's more a coincidence.

For array inspection, you should either use xdebug or, if you don't have time, patience or skill to set it up, use Devel module. Specifically, dpm, dvm, dpr and dvr functions will be useful for you. See this helpful demo page for details of their use.

TL;DR: Install Devel, replace print and print_r() calls with dvm(). Profit.

| improve this answer | |
  • 1
    I have removed the comments. Please don't use them for extended discussion. – kiamlaluno Jan 20 '16 at 15:28

print() and echo() are not good for debugging, they are like the document.write() equivalent.

print_r() is like the console.log() equivalent. I prefer to use var_dump() as the output formatting is much better, providing HTML Error Display is turned on in the php.ini file.

To test if the PHP execution is getting to the place where you are debugging put in a die() after the print_r():

print_r($your_variable); die('123');

Now check if you see '123' on the screen. If the execution stops it's good because you know your debugging code is executing.

You can also install the devel module to use dvm() or dpm(), the difference between them is the formatting. dvm() is formatted similar to print_r() and dpm uses krumo formatting (if that option is set in the Devel settings). These functions will print out in the $messages variable, so make sure the theme's page.tpl file is printing $messages.

If nothing is printed on the screen after a print_r() it could be a number of reasons, here's 2 common reasons I have experienced.

  1. There is a PHP error and your php.ini file is set so no errors are displayed. (blank white screen).

  2. If the site renders but your debugging code is not displayed, it's possible your code is not included in the execution of the page you refreshed.

| improve this answer | |

What I managed to get working was using: drupal_set_message(); I couldn't get dpm(), dvm(), or dpr() to work after installing the Devel contrib module. I was able to print out a variable using die() but this also stops all execution and makes Drupal unusable during the test so is not desirable.

If anyone has a better solution please let me know and ill pick the better answer but so far drupal_set_message(); is the only thing that is working.

I have some json data that i'm currently testing and found the best way to work with it is:

$data = json_decode($rawData,true);
foreach($data as $key => $val){
   drupal_set_message($key." = ".$val);

This way i was able to see each level of the json data and work on one part of it at a time. There must be a more effective way to see complex data as your building a module.

| improve this answer | |
  • Yes, die() will stop the execution, it's just to test if the execution is getting to that part in the code. As for the devel module, you may need to enable some settings to get the debugger functions to work, and the debugging will print out in the $messages variable so the theme page.tpl file has to be calling that variable – Rick Bergmann Jan 19 '16 at 23:37
  • dpm() and drupal_set_message() are equivalent. dpm() does depend on the user having the "access devel information" permission. See api.drupal.org/api/devel/devel.module/function/dpm/7 – rooby Jan 20 '16 at 0:08
  • What settings do i need to change to make devel work? – Patrick W. McMahon Jan 20 '16 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.