This is must be a silly question, but how do I dump $node?

i tried print $node;
and get:
recoverable fatal error: Object of class stdClass could not be converted to string

I tried print_r $node;
and get

Parse error: syntax error, unexpected T_VARIABLE

I am trying to solve this problem : http://drupal.org/node/350676

Thanks for any insight

Comments

baldwinlouie’s picture

you can use var_dump($node);

Baldwin Louie,
BitSprout LLC
http://www.bitsprout.net

yelvington’s picture

print_r() is a function, not a keyword. Read the PHP manual.

dman’s picture

print_r() is a function.
Functions take their arguments in (brackets).
You'll see a lot of examples if you look at PHP code...

.dan.
if you are asking a question you think should be documented, please provide a link to the handbook where you think the answer should be found.
| http://www.coders.co.nz/ |

jimthunderbird’s picture

Try print_r($node);

extexan’s picture

I'm having the same problem... neither of those suggestions worked (print_r or var_dump).

I first tried this (as suggested on Drupal.org):

<pre>
  <?php print_r($node); ?>
</pre>

When I saw nothing different on my first page, I added echos...

<pre>
  <?php echo '<br />Got here<br />'; ?>
  <?php var_dump($node); ?>
  <?php echo '<br />Got here 2<br />'; ?>
</pre>

I then saw "Got here" followed by "Got here 2" with nothing in between. When I changed the print_r to var_dump, the only difference was that "NULL" appeared between the two "Got here"s.

I know the code is executing, or I wouldn't see the echos. Why am I not seeing the value(s) of $node?

It's never too late to have a happy childhood. ;-)

grobemo’s picture

print_r($node) should work when $node is defined. I've used that method often. Doublecheck that $node is defined:

<?php
  if (empty($node)) {
    print '$node is empty';
  }
  else {
    print '$node is defined';
  }
?>

Where are you trying to execute this code?

You might also take a look at the Devel module, which enables you to inspect node objects easily.

extexan’s picture

Pardon my noobie-ness, but I did as instructed on this page (http://drupal.org/node/348916). It said to put that code (the print_r) at the top of *any* template (.tpl.php) file in my theme.

My theme is actually a sub-theme of Zen - I started with the STARTERKIT folder as instructed and renamed it to litdep (my sub-theme). The code I added was at the top of sites/all/themes/zen/litdep/template.php.

So, when and where is $node defined? Since I am just delving into Drupal, I'm trying to find out what global variables are available to me. Besides $node, what else should I be looking at? I've read about the preprocessors, and the $variables array, but doing a print_r on that didn't yield anything either.

Btw, the link in ur message to "Devel module" was a no go (no show). But I've seen references to it in the Drupal docs, so I'm sure I'll be able to find it.

And, btw 2, thanks for your help.

It's never too late to have a happy childhood. ;-)

grobemo’s picture

Sorry for the bum link: The Devel module is at http://drupal.org/project/devel.

This is a common confusion, but (though the name certainly suggests otherwise) template.php is actually not a "template file" in the relevant sense. (It doesn't end in .tpl.php, but only in .php.) The template files are page.tpl.php, etc. Try adding print_r($node) to the top of page.tpl.php. (But note that it might be enclosed in <?php ?> tags.)

It's hard to give a general statement about the contexts in which $node is defined. You can always define it yourself (if you know the nid of the node you want) by calling node_load().

You can also figure out which variables are defined in any given context with this code:

<?php
  print '<pre>';
  print_r(get_defined_vars());
  print '</pre>';
?>

Zen also has pretty good documentation about what's available.