PHP Var_Dump() VS Print_R()

php var_dump() vs print_r()

The var_dump function displays structured information about variables/expressions including its type and value. Arrays are explored recursively with values indented to show structure. It also shows which array values and object properties are references.

The print_r() displays information about a variable in a way that's readable by humans. array values will be presented in a format that shows keys and elements. Similar notation is used for objects.

Example:

$obj = (object) array('qualitypoint', 'technologies', 'India');

var_dump($obj) will display below output in the screen.

object(stdClass)#1 (3) {
[0]=> string(12) "qualitypoint"
[1]=> string(12) "technologies"
[2]=> string(5) "India"
}

And, print_r($obj) will display below output in the screen.

stdClass Object ( 
[0] => qualitypoint
[1] => technologies
[2] => India
)

More Info

  • var_dump
  • print_r

Difference between var_dump,var_export & print_r

var_dump is for debugging purposes. var_dump always prints the result.

// 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")}
}

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. print_r by default prints the result, but allows returning as string instead by using the optional $return parameter.

Array (
[0] =>
[1] =>
[2] => 42
[3] => Array ([0] => 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. var_export by default prints the result, but allows returning as string instead by using the optional $return parameter.

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

Personally, I think var_export is the best compromise of concise and precise.

How do I properly use print_r or var_dump?

var_dump always shows you an array in formatted data, but too much extra stuff

var_dump($data);

But if you want formatted data, here you need to use <pre> tags:

echo '<pre>';
print_r($data);
echo '</pre>';

What is the difference between print_r and echo when providing results to an AJAX call?

The most common built-in output functions in PHP are: echo, print, printf, print_r, var_dump, and var_export. They are all designed to output data, albeit, in slightly different ways. Which is why there are specific use cases associated with each function.

  • echo: The most notorious. It's what you should be using to output data to an AJAX caller. It's simple and effective. It's actually a language construct, so it doesn't require you to use brackets. You just write echo 'string'; or echo $something;, etc.

    It also supports multiple comma-delimited values, making it handy in a variety of use cases; e.g., echo 'string1', 'string2', 'string3';. Note that echo returns null (nothing).


  • print: Also a language construct, it's nearly the same as echo, with the difference being that it accepts only one argument and always returns the same thing: 1. It's OK to use print instead of echo if you like, but many popular PHP projects have code standards that ask you not to do so. So print has become, more or less, an unused (or rarely used) function in PHP.

  • printf: This function and its brother sprintf() are designed to output formatted strings. It's slightly more complex, but does have specific use cases. For example, these are often used to output an integer as a binary number or octal number, for translation, among many other options that are well documented at PHP.net — that's a separate topic.

  • print_r, var_dump, and var_export: These are most commonly used when debugging code or testing code in one way or another. You will rarely find a use case for them in production code. That said, these allow you to pass in complex data types, such as an entire array, and print_r recursively, to dump the entire array for analysis. var_dump does pretty much the same thing, but uses a slightly different approach that can sometimes be useful. var_export has the advantage of outputting a parseable string.

    print_r and var_export both support a parameter that can 'return' the value it would normally output, which can be a handy feature. Something that var_dump is not capable of.

Not var_dump() nor print_r() will show readable information.... but the same confusing output. Why?

try to preformat it for better readability:

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

Is there a print_r or var_dump equivalent in PHP that does not echo the result by default?

Why not create a custom function that will wrap all your print_r calls? I use something like this:

function good_print() {
$log = '';
foreach(func_get_args() as $arg) $log .= print_r($arg, true);
return $log;
}

It saves time, gives me better functionality, plus I don't have to worry about "did I use the right call this time?"

var_dump or print_r and html encoding

I found that knittl's code does not work. I had to make some small changes to get it to work as follows:

array_walk_recursive($inputarray, function(&$v) { $v = htmlspecialchars($v); });

Now this works fine in PHP5.3+

php var_dump equivelant or print_r

print_r has the option for a second parameter. When set to true, it returns the dump as an array instead of displaying it.

http://us2.php.net/print_r



Related Topics



Leave a reply



Submit