PHP Reference ยท Functions and Classes
PHP var_dump(), print_r() and debug helpers
Reference for PHP var_dump(), print_r(), var_export() and debug_zval helpers: show types and values, return output as a string and read the output.
When you need to see what a variable really holds, PHP offers a few output functions. var_dump() prints the type and value with sizes, print_r() prints a compact, human-readable form, and var_export() prints valid PHP code. Each answers a slightly different question.
var_dump() is the debugging workhorse: it tells "1" from 1, false from null, and shows exact float values. print_r() is shorter but hides those differences.
var_dump(mixed $value, mixed ...$values): void print_r(mixed $value, bool $return = false): string|bool
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
var_dump(mixed $value, mixed ...$values): void print_r(mixed $value, bool $return = false): string|bool var_export(mixed $value, bool $return = false): ?string debug_print_backtrace(int $options = 0, int $limit = 0): void debug_backtrace(int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT, int $limit = 0): array
| Function | Output |
|---|---|
var_dump() |
Type, length and value, one per argument. Objects show object(Class)#id (count) { ... } with property visibility. |
print_r() |
Array ( [key] => value ) style. With $return = true it returns the text instead of printing. |
var_export() |
Valid PHP code. |
debug_print_backtrace() / debug_backtrace() |
The current call stack, printed or as an array. |
Return value: var_dump() returns nothing. print_r() returns true after printing, or the string if $return is true.
Usage notes
var_dump()accepts several arguments and prints each in turn. It has noreturnflag; capture it with output buffering (ob_start()/ob_get_clean()), as the second example does.print_r()loses information:falseandnullprint as an empty string,trueas1, and1and"1"look alike. Use it for readable structure andvar_dump()for exact types.- Object ids.
var_dump()shows#1,#2and so on: handles assigned in creation order. They are not stable identifiers, so do not use them in tests. - Visibility is shown. Both functions reveal private and protected properties;
var_dump()marks them as["y":protected]and["label":"Point":private]. - Floats are printed with their shortest exact form (
float(0.30000000000000004));NAN,INFand-0are shown by name. - In a browser wrap output in
<pre>(or use a debugger) to keep line breaks; never leave dumps on a production page, where they leak data. - Recursion is marked
*RECURSION*instead of looping forever. __debugInfo()lets a class control whatvar_dump()andprint_r()show, for example to hide secrets.- Better tools. A step debugger (Xdebug) or a logger (
error_log()) is safer than dumping in real applications.
Related
var_export(),serialize(),gettype()andget_debug_type(),echo- Lessons: Debugging, Errors