views:

899

answers:

3

I use echo and print_r much,almost never use print.

I feel echo is a macro,and print_r is alias of var_dump.

But that's not the standard way to explain the difference.

+4  A: 

print does the same thing as echo except echo is a language construct and not a real function so it's syntax is different. (For what it's worth, I've never used print.)

var_dump prints out a detailed dump of a variable, including its type and the type of any sub-items (if it's an array or an object). print_r prints a variable in a more human-readable form: strings are not quoted, type information is omitted, array sizes aren't given, etc.

var_dump is usually more useful than print_r when debugging, in my experience. It's particularly useful when you don't know exactly what values/types you have in your variables. Consider this test program:

$values = array(0, false, '');

var_dump($values);
print_r ($values);

With print_r you can't tell the difference between 0 and 0.0, or false and '':

array(4) {
  [0]=>
  int(0)
  [1]=>
  float(0)
  [2]=>
  bool(false)
  [3]=>
  string(0) ""
}

Array
(
    [0] => 0
    [1] => 0
    [2] => 
    [3] => 
)
John Kugelman
I've never used print either (over echo).
cletus
What is language constructure?
Shore
@Shore - it's something built into the language that isn't a function. They are generally very fast, and don't operate exactly like normal functions.
alex
`print` is also a language construct. Wikipedia defines it as: "a syntactically allowable part of a program that may be formed from one or more lexical tokens in accordance with the rules of a programming language".
nico
This answer contains some inaccuracies - not only is print indeed a language construct just like echo, but they don't do "exactly the same thing". Print returns a value, so it can be used in an expression while echo doesn't. Echo allows echoing more than one string separated by commas while print doesn't.
thomasrutter
Ben Rowe
@thomasrutter: just wanted to say the same thing :) +1
exhuma
+2  A: 

Just to add to John's answer, echo should be the only one you use to print content to the page.

print is slightly slower. var_dump and print_r should only be used to debug.

Also worth mentioning is that print_r will echo by default, add a second argument that evaluates to true to get it to return instead, e.g. print_r($array, true)

The difference between echoing and returning are:

  • echo: Will immediately print the value to the output
  • returning: Will return the function's output as a string. Useful for logging, etc
alex
Just because you sort of raised the issue, what's the difference between `echo`ing and `return`ing?
David Thomas
wow I wish I knew about the returning parameter :(basically you can do $foo = print_r($array, true); and use it in other ways (into a log, table, etc)
FryGuy
I've used the `print_r` returning param quite a lot while I was coding PHP. However, I tended to write `print_r( $foo, 1 );`. It's quicker to type ;) And about this on I don't care much about readability as I find the name `print_r` not very descriptive either.
exhuma
+3  A: 

echo

  • No return value
  • Outputs one or more strings separated by commas

    e.g. echo "String 1", "String 2"

print

  • Returns 1, so it can be used in an expression
  • Outputs only a single string

    e.g. if ((print "foo") && (print "bar"))

print_r()

  • Outputs any variable (not just strings but even arrays, objects) as a formatted, human-readable string representation. Good for debugging. It's a more human-readable serialization than serialize().
  • No return value, unless using the second optional argument, which causes it to return a string containing what would have been output instead of outputting it.

Notes:

  • Whereas echo and print are language constructs, print_r() is a function. This means that using parantheses to enclose the arguments for echo and print can behave in unintuitive ways sometimes.
thomasrutter