Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress Custom Menu Development in WordPress The Walker Class for WordPress Customizing the Walker Class to Include Custom Menu Item Fields

Collin Berg
Collin Berg
33,121 Points

Var_dump vs Var_export?

In the video he uses var_export to get the value of $item. However, since we're doing debugging/ testing to see if our item is being retrieved properly, shouldn't we be using var_dump? When should we use one over the other?

1 Answer

Matthew Smart
Matthew Smart
12,567 Points

var_dump is for debugging purposes. http://php.net/var_dump

// var_dump(array('', false, 42, array('42')));
array(4) {
   [0]=> string(0) ""
   [1]=> bool(false)
   [2]=> int(42)
   [3]=> array(1) {[0]=>string(2) "42")}
 }

var_export prints valid php code. Useful if you calculated some values and want the results as a constant in another script. Note that var_export can not handle reference cycles/recursive arrays, whereas var_dump and print_r check for these. http://php.net/manual/en/function.var-export.php

array (
   0 => '',
   2 => false,
   2 => 42,
   3 => array (0 => '42',),
)

print_r is for debugging purposes, too, but does not include the member's type. It's a good idea to use if you know the types of elements in your array, but can be misleading otherwise: http://php.net/print_r

Array (
     [0] =>
     [1] =>
     [2] => 42
     [3] => Array ([0] => 42)
)

Personally, the one I use most often is print_r() with pre tags like seen below:

echo "<pre>";
print_r($variable);
echo "</pre>";

The Pre tags make it visually more appealing.

Collin Berg
Collin Berg
33,121 Points

So print_r is just a cleaner version of var_dump?