Skip to content
Tutorials Center
My learning

Topics

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.

PHP
var_dump(mixed $value, mixed ...$values): void
print_r(mixed $value, bool $return = false): string|bool

Try it

PHP var_dump versus print_r

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.

PHP Capturing output and dumping objects

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

PHP
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 no return flag; capture it with output buffering (ob_start()/ob_get_clean()), as the second example does.
  • print_r() loses information: false and null print as an empty string, true as 1, and 1 and "1" look alike. Use it for readable structure and var_dump() for exact types.
  • Object ids. var_dump() shows #1, #2 and 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, INF and -0 are 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 what var_dump() and print_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.