Difference between var_dump and print_r

Asked

Viewed 3,967 times

6

I have a simple and objective question: Always in my research around, I notice that some programmers use var_dump() and others use print_r (like me).

What’s the difference between print_r() and var_dump(), both of which bring "practically" the same result?

1 answer

6


Basically the differences between print_r()and var_dump() are that the second in addition to displaying the value/structure of the variable shows its type and size in the case of strings. Do not return expression/variable value in this case var_export() is recommended.

var_dump()

$arr = array('a', 'b', 'c', 'teste');
var_dump($arr);

Exit:

array (size=4)
  0 => string 'a' (length=1)
  1 => string 'b' (length=1)
  2 => string 'c' (length=1)
  3 => string 'teste' (length=5)

print_r()

$arr = array('a', 'b', 'c', 'teste');
print_r($arr);

Exit:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => teste
)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.