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.
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.
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] =>
)
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
Outputs one or more strings separated by commas
e.g. echo "String 1", "String 2"
1
, so it can be used in an expressionOutputs only a single string
e.g. if ((print "foo") && (print "bar"))
print_r()
Notes: