Press "Enter" to skip to content

The Difference Between var_export and var_dump

How the problem was found

While tracing yratings_get_targets, I ran:

   error_log(var_export(yblog_mspconfiginit("ratings"),true));

and it kept printing that the return value of yblog_mspconfiginit("ratings") was NULL,
which led me to think the DB connection couldn't be established. That sent me down the wrong path for a whole day.
In the end I discovered this is one of the differences between var_export and var_dump.
Namely:

The cause

var_export must return valid PHP code — that is, the code var_export returns can be assigned directly to a variable as PHP code, and that variable will then hold a value of the same type as the one var_export was given.

  • However, when the variable is of type resource, it can't be simply copied. So when var_export is given a resource, var_export returns NULL.

Examples

$res = yblog_mspconfiginit("ratings");
var_dump($res);
var_export($res);

Result:

resource(1) of type (yahoo_yblog)
NULL

Another example:

$res = fopen('status.html', 'r');
var_dump($res);
var_export($res);

Result:

resource(2) of type (stream)
NULL

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.