14 Replies - 5768 Views - Last Post: 06 March 2012 - 09:58 AM

#1 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

var_dump vs print_r

Posted 02 November 2011 - 07:14 PM

I see both everywhere, but I have no idea the difference. The php docs are basically the same for both, so what's the difference (if there is any)?
Is This A Good Question/Topic? 0
  • +

Replies To: var_dump vs print_r

#2 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: var_dump vs print_r

Posted 02 November 2011 - 10:13 PM

There isn't a whole lot of difference. Just look at the output examples in the documentation - var_dump() outputs more type information. Also, if you use xdebug, var_dump() gets over-ridden to add some extra functionality. But that's about it - as you say, they're very similar.

Don't worry about it. It's just another case of PHP having multiple functions that do almost exactly the same thing, but not quite, for no discernable reason.
Was This Post Helpful? 4
  • +
  • -

#3 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: var_dump vs print_r

Posted 04 November 2011 - 07:11 AM

I usually use print_r, I find it more readable. var_dump will give you some extra info, data type, string lengths etc, but for 95% of the time I find print_r better simply because it conveys the information I need in a more readable format.

You can see the difference with this simple example
	$arr = array("First_Key"=>"first_value","Second_Key"=>"second_value",5,2,"another_value");
	$obj = new stdClass();
	$obj->first_prop = "Hello";
	$obj->second_prop = "World!";
	
	echo "array print_r : <br/>";
	print_r($arr);
	echo "<br />";
	echo "object print_r : <br/> ";
	print_r($obj);
	
	echo "<br />";
	echo "array var_dump: <br/>";
	var_dump($arr);
	echo "<br />";
	echo "object var_dump : <br/> ";
	var_dump($obj);



When you start implementing OOP composition with objects that have properties that are other objects which have properties that are other objects... and so on, readability becomes much more of an issue when you are trying to view the contents of said objects.
Was This Post Helpful? 2
  • +
  • -

#4 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: var_dump vs print_r

Posted 19 November 2011 - 03:44 PM

Ah, thanks.
Was This Post Helpful? 0
  • +
  • -

#5 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: var_dump vs print_r

Posted 20 November 2011 - 03:26 AM

var_dump is most useful when you're trying to pass across parameters to somewhere that need to be of a certain type simply because it shows the type information.

It's actually the only difference between the two functions as mentioned before, and from a personal standpoint I always try to use var_dump where possible, as it's always nice to have the correct typeness.

To be honest, I like to see when I've got a string of "1" to deal with vs an int of 1. But perhaps that's just me.
Was This Post Helpful? 2
  • +
  • -

#6 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: var_dump vs print_r

Posted 27 November 2011 - 10:33 AM

One feature of print_r() worth noting as well is its second argument. If you pass true as the second argument, print_r()'s output is returned instead of directly output. var_dump() cannot do this, but var_dump() does do a bit more work useful for debugging that print_r() doesn't. It all comes down to what you're doing and which one makes the most sense for your situation.

This post has been edited by Valek: 27 November 2011 - 10:34 AM

Was This Post Helpful? 1
  • +
  • -

#7 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 170
  • View blog
  • Posts: 608
  • Joined: 12-October 09

Re: var_dump vs print_r

Posted 30 November 2011 - 11:49 AM

I used to use print_r, but since I started using var_dump i've found it much better and easier to read.
Was This Post Helpful? 0
  • +
  • -

#8 hockey97  Icon User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

Re: var_dump vs print_r

Posted 25 February 2012 - 10:19 AM

In my opinion... I use var_dump for debugging purposes. I use print_r mainly to help debug arrays. I don't know if var_dump is better at debugging arrays.

Yet, if you want to be technical their isn't really much a difference. It's just that var_dump is more geared towards debugging variables rather then print_r.
Was This Post Helpful? -1
  • +
  • -

#9 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: var_dump vs print_r

Posted 25 February 2012 - 02:38 PM

View Posthockey97, on 25 February 2012 - 05:19 PM, said:

Yet, if you want to be technical their isn't really much a difference. It's just that var_dump is more geared towards debugging variables rather then print_r.

???????????

No, one of them is not "more for debugging". One of them does however provide more information to you, as it gives you the type of variables that it is "dumping". Whether or not this is more for debugging depends on the context.
Was This Post Helpful? 3
  • +
  • -

#10 hockey97  Icon User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

Re: var_dump vs print_r

Posted 28 February 2012 - 12:03 PM

View PostRudiVisser, on 25 February 2012 - 10:38 PM, said:

View Posthockey97, on 25 February 2012 - 05:19 PM, said:

Yet, if you want to be technical their isn't really much a difference. It's just that var_dump is more geared towards debugging variables rather then print_r.

???????????

No, one of them is not "more for debugging". One of them does however provide more information to you, as it gives you the type of variables that it is "dumping". Whether or not this is more for debugging depends on the context.


ya, it's is recommended var_dump to be used for debugging. I am just sharing what I been told. Don't need to go all postal on me. Google the subject and you will find many websites suggesting to use Var_dump to be used when your debugging your code rather then print_r.
Was This Post Helpful? 0
  • +
  • -

#11 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: var_dump vs print_r

Posted 06 March 2012 - 03:19 AM

View Posthockey97, on 29 February 2012 - 05:03 AM, said:

Google the subject and you will find many websites suggesting to use Var_dump to be used when your debugging your code rather then print_r.

At the risk of incurring negative rep for no good reason, I Googled "php var_dump vs print_r", and none of the first ten results suggest using one over the other.
Was This Post Helpful? 0
  • +
  • -

#12 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: var_dump vs print_r

Posted 06 March 2012 - 08:58 AM

It's not a either-or situation in any case. Like a lot of people have said already, the functions provide different output useful for different situations. Recommending either one over the other for "debugging" in general is rather short-sighted.

If I were to recommend anything, it would be to set up XDebug and have it interface with one of the IDEs that can do actual "debugging", like Netbeans. For large projects, the ability to set breakpoints alone is more that worth it.

View Posthockey97, on 28 February 2012 - 07:03 PM, said:

I am just sharing what I been told.

You may want to consider verifying what these people are telling you before passing it on as truth. It's clear that it's not all solid fact.
Was This Post Helpful? 1
  • +
  • -

#13 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: var_dump vs print_r

Posted 06 March 2012 - 09:37 AM

Is that even possible? I've never heard of setting breakpoints to debug PHP.
Was This Post Helpful? 0
  • +
  • -

#14 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: var_dump vs print_r

Posted 06 March 2012 - 09:46 AM

Yes, you can do that with XDebug. See this page for more details on how it works:

Debugging PHP Source Code in the NetBeans IDE
Was This Post Helpful? 2
  • +
  • -

#15 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: var_dump vs print_r

Posted 06 March 2012 - 09:58 AM

That's amazing. I've always wished something like that existed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1