2
print_r(array('name'=> 'bob', 'age' => 20, 'sex' => 'man'));

Then :

Array {
    [name] => bob,
    [age] => 20,
    [sex] => man }

var_dump(array('name' => 'bob', 'age' => 20));

will display:

array(2) {
    ['name'] => string(3) 'bob'
    ['age'] => int(20) }

var_dump is perfect to debug and much better than print_r. But why print_r still exists? or print_r has some advantage i don't know

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
ShunnarMeng
  • 115
  • 1
  • 1
  • 8

4 Answers4

11

If you output print_r between to <pre> tags then it gives you a much more readable output than var_dump. That is my main reason for using while debugging.

Array
(
    [name] => bob,
    [age] => 20,
    [sex] => man
)

Basically it gives you a formatted output that var_dump doesn't. Although it doesn't give you quite as detailed type information.

Coin_op
  • 10,568
  • 4
  • 35
  • 46
8

A big difference between print_r and var_dump is that print_r takes an optional second argument, which allows you to the store the contents in a variable. For example:

$debug = print_r($someArray, true);
echo $debug;

(Note that this could also be achieved for var_dump using output control functions, though)

Additionally, the readability of print_r is far better than that of var_dump:

var_dump:

array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  array(3) {
    [0]=>
    string(1) "a"
    [1]=>
    string(1) "b"
    [2]=>
    string(1) "c"
  }
}

print_r:

Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )
)
Aron Rotteveel
  • 81,193
  • 17
  • 104
  • 128
2

Can you suggest some reason to break hundreds of thousands of PHP scripts by removing it?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

If you wish to get as much data as you can use var_dump(), however i find that print_r() is more readable by humans.

BigFatBaby
  • 1,525
  • 9
  • 19