Skip to content

Instantly share code, notes, and snippets.

@dotherightthing
Created August 15, 2019 08:43
Show Gist options
  • Save dotherightthing/fa154026945c9c8bb8996db1b5ae15e3 to your computer and use it in GitHub Desktop.
Save dotherightthing/fa154026945c9c8bb8996db1b5ae15e3 to your computer and use it in GitHub Desktop.
[print_r vs var_dump] #php

print_r vs var_dump

Created: 2017.04.18

I've always used print_r, but some tutorials use var_dump.

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.

and

Generally, print_r( ) output is nicer, more concise and easier to read, aka more human-readable but cannot show data types.

With print_r() you can also store the output into a variable.

var_dump is better for debugging

When requesting data via an API, var_dump is preferable.

Using var_dump reveals the data types used, so we can easily check whether the data is in the expected format - or whether we need to convert it, using e.g. json_decode.

We can also see useful information like how many items are in an array, and which index an array item has.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment