What is var_dump?
The var_dump manual page [ http://php.net/var_dump ] states that var_dump displays structured information about one or more expressions that includes its type and value.
Arrays and objects are explored recursively with values indented to show structure.
Description :
[ad type=”banner”]This function displays structured information about one or more expressions that includes its type and value.
Arrays and objects are explored recursively with values indented to show structure.
All public, private and protected properties of objects will be returned in the output unless the object implements a __debugInfo()
[ http://us3.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo ] method (implemented in PHP 5.6.0).
Example #1
var_dump() example:
The above example will output:
The problem is that var_dump outputs its result directly to the browser, how can you capture its output in a string variable? read on…
Using output control functions for the solution:
Output control functions can be used to capture and redirect the standard output. For more details, read the PHP manual on output control functions
[ http://php.net/manual/en/ref.outcontrol.php ], of course.
Sample code:
Use output buffering:
A smart alternative to PHP’s “var_dump” function
The “var_debug” function is a smart alternative to PHP’s “var_dump” function, which limits the output and prints the file path and line of the invocation.
File path and line of invocation
The “var_debug” function outputs the file path and line number of the invocation. So even when your “var_dump” calls all say the same you can still see what happened. To accomplish this, it uses the PHP “debug_backtrace” function.
It sets the “DEBUG_BACKTRACE_IGNORE_ARGS” option to reduce memory usage. From the produced backtrace, it gets the first element that holds a file path and line number and it adds that to the start of the output.
Limit the output:
The “var_debug” function limits the output with the following default rules:
Only the first 100 characters of each string are logged (full length is shown)
Only the first 25 elements of an array are logged (full length is shown)
Only the first 10 levels of nested objects/arrays are logged
These three limits can be set using three optional parameters in the above order. Hence, calling “var_debug($variable)” is equal to calling “var_debug($variable,100,25,10)”.
Modifications:
If you would like the function to echo HTML-printable output, you can replace the last line that says “echo $string;” with the following:
Replace “echo $string;” with “return $string;” if you want to use the output for logging purposes in some other function.
var_export
You may want to check out var_export — while it doesn’t provide the same output as var_dump it does provide a second $return parameter which will cause it to return its output rather than print it:
[ad type=”banner”]The difference between var_dump and var_export is that var_export returns a “parsable string representation of a variable” while var_dump simply dumps information about a variable.
What this means in practice is that var_export gives you valid PHP code.
Demo:
The difference in output:
var_export ($debug_export in above example)
var_dump ($debug_dump in above example)
print_r ($debug_printr in above example):
Caveat: var_export does not handle circular references
If you’re trying to dump a variable with circular references, calling var_export will result in a PHP warning:
Results in:
[ad type=”banner”]Both var_dump and print_r, on the other hand, will output the string *RECURSION* when encountering circular references.
good article