Difference between var_dump and print_r

4

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

  

What is the difference between print_r() and var_dump() , both   bring "pretty much" the same result?

    
asked by anonymous 21.12.2016 / 12:37

1 answer

4

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 value of the expression / variable in this case var_export () is recommended.

var_dump ()

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

Output:

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);

Output:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => teste
)
    
21.12.2016 / 12:46