Difference Between Var_Dump,Var_Export & 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.

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

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.

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

What would cause a print_r and/or a var_dump to fail debugging a variable?

First, PHP never "just white pages". When you get a blank screen, that means PHP's execution has halted fro some reason. However, unless your server has been configured to not log errors, the PHP error log or the Magento exception log should have an error for you.

As far as your specific problem goes, many of Magento's objects contain reference to a large amount of information — and sometimes the references are circular. PHP's var_dump and print_r functions will blindly follow these circular references and attempt to print everything out. This eventually leads to PHP using more memory than is allowed by the memory_limit ini setting, and execution halts.

Most PHP professionals use the xDebug extension to work around this. The xDebug extension has a modified var_dump that will limit the amount of information dumped, which prevents the above memory limit problems. The xdebug.var_display_max_children, xdebug.var_display_max_data, and xdebug.var_display_max_depth ini settings are the ones you'll want to tweak if xDebug's still not helping with the memory limit problem. (some PHP distributions have these set too high initially)

If that's not a possibility, a little caution with your var_dump's can still help.

Use this to figure out the variable type

var_dump(get_class($thing));

If it's a Magento object, use this to see its data keys

var_dump(array_keys($thing->getData()));

And then output individual data members with

var_dump($thing->getData('key_name'));
var_dump($thing->getKeyName()));

Why print_r and var_dump shows private member values?

It's not a bug, it is just showing the structured information about the data you are providing. Showing private property doesn't mean that it is making them accessible from outside. It just giving the information.

This is perfectly normal.

PHP's var_dump / print_r output is garbled - encoding issue?

For what it's worth, I finally got to the bottom of this problem (I think!)

The problem seems to be that the API's output was being run through json_decode whether it was JSON or not. MySQL errors were causing an error page, not a JSON response, which when run through json_decode (by the API-handling code that received it) before var_dump produced garbled character salad, as above.

How can I capture the result of var_dump to a string?

Use output buffering:

<?php
ob_start();
var_dump($someVar);
$result = ob_get_clean();
?>


Related Topics



Leave a reply



Submit