I use echo
and print_r
much, and almost never use print
.
I feel echo
is a macro, and print_r
is an alias of var_dump
.
But that's not the standard way to explain the differences.
I use echo
and print_r
much, and almost never use print
.
I feel echo
is a macro, and print_r
is an alias of var_dump
.
But that's not the standard way to explain the differences.
print
and echo
are more or less the same; they are both language constructs that display strings. The differences are subtle: print
has a return value of 1 so it can be used in expressions whereas echo
has a void
return type; echo
can take multiple parameters, although such usage is rare; echo
is slightly faster than print
. (Personally, I always use echo
, never 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, 0.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] =>
)
echo
No return value
e.g. echo "String 1", "String 2"
Returns 1
, so it can be used in an expression
e.g. print "Hello"
or, if ($expr && print "foo")
print_r()
var_dump()
print_r()
, for example it also prints the type of valuesvar_export()
print_r()
and var_dump()
- resulting output is valid PHP code!Notes:
print
can be used in an expression, I recommend people avoid doing so, because it is bad for code readability (and because it's unlikely to ever be useful). The precedence rules when it interacts with other operators can also be confusing. Because of this, I personally don't ever have a reason to use it over echo
.echo
and print
are language constructs, print_r()
and var_dump()
/var_export()
are regular functions. You don't need parentheses to enclose the arguments to echo
or print
(and if you do use them, they'll be treated as they would in an expression).var_export()
returns valid PHP code allowing values to be read back later, relying on this for production code may make it easier to introduce security vulnerabilities due to the need to use eval()
. It would be better to use a format like JSON instead to store and read back values. The speed will be comparable.echo $a, $b;
syntax when you can just do echo $a . $b;
: if either $a or $b are really large strings then the latter will use extra memory and time creating a separate concatenated version of the strings in memory before it can start outputting either to the browser. –
Mapes echo $a; echo $b;
too. –
Mapes 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()
and var_dump()
will echo by default, add a second argument to print_r()
at least that evaluates to true to get it to return instead, e.g. print_r($array, TRUE)
.
The difference between echoing and returning are:
echo
ing and return
ing? –
Itin 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. –
Sorrel var_dump()
doesn't support the second argument allowing it to return a value like print_r()
does, because var_dump()
can accept multiple arguments to output. –
Mapes The difference between echo, print, print_r and var_dump is very simple.
echo
echo is actually not a function but a language construct which is used to print output. It is marginally faster than the print.
echo "Hello World"; // this will print Hello World
echo "Hello ","World"; // Multiple arguments - this will print Hello World
$var_1=55;
echo "$var_1"; // this will print 55
echo "var_1=".$var_1; // this will print var_1=55
echo 45+$var_1; // this will print 100
$var_2="PHP";
echo "$var_2"; // this will print PHP
$var_3=array(99,98,97) // Arrays are not possible with echo (loop or index value required)
$var_4=array("P"=>"3","J"=>"4"); // Arrays are not possible with echo (loop or index value required)
You can also use echo statement with or without parenthese
echo ("Hello World"); // this will print Hello World
Just like echo construct print is also a language construct and not a real function. The differences between echo and print is that print only accepts a single argument and print always returns 1. Whereas echo has no return value. So print statement can be used in expressions.
print "Hello World"; // this will print Hello World
print "Hello ","World"; // Multiple arguments - NOT POSSIBLE with print
$var_1=55;
print "$var_1"; // this will print 55
print "var_1=".$var_1; // this will print var_1=55
print 45+$var_1; // this will print 100
$var_2="PHP";
print "$var_2"; // this will print PHP
$var_3=array(99,98,97) // Arrays are not possible with print (loop or index value required)
$var_4=array("P"=>"3","J"=>"4"); // Arrays are not possible with print (loop or index value required)
Just like echo, print can be used with or without parentheses.
print ("Hello World"); // this will print Hello World
print_r
The print_r() function is used to print human-readable information about a variable. If the argument is an array, print_r() function prints its keys and elements (same for objects).
print_r ("Hello World"); // this will print Hello World
$var_1=55;
print_r ("$var_1"); // this will print 55
print_r ("var_1=".$var_1); // this will print var_1=55
print_r (45+$var_1); // this will print 100
$var_2="PHP";
print_r ("$var_2"); // this will print PHP
$var_3=array(99,98,97) // this will print Array ( [0] => 1 [1] => 2 [2] => 3 )
$var_4=array("P"=>"3","J"=>"4"); // this will print Array ( [P] => 3 [J] => 4 )
var_dump
var_dump function usually used for debugging and prints the information ( type and value) about a variable/array/object.
var_dump($var_1); // this will print int(5444)
var_dump($var_2); // this will print string(5) "Hello"
var_dump($var_3); // this will print array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }
var_dump($var_4); // this will print array(2) { ["P"]=> string(1) "3" ["J"]=> string(1) "4" }
echo
Not having return type
print
Have return type
print_r()
Outputs as formatted,
Echo :
It is statement not a function No return value
Not Required the parentheses
Not Print Array
It is real function
Return type 1
Required the Parentheses
Not Print Array
Print_r
Print in human readable format
String not in Quotes
Not Detail Information of Variable like type and all
var_dump
All dump information of variable like type of element and sub element
print
is not a real function, it is a language construct just like echo. It can also be used as a statement. echo
is not a statement nor can it be used in one. print
does not require parentheses, nor do either echo or print use the parentheses like a function would. You also missed one of the main differences - echo accepts multiple strings to echo separated by commas. –
Mapes **Echocan accept multiple expressions while print cannot. The Print_r () PHP function is used to return an array in a human readable form. It is simply written as
![Print_r ($your_array)][1]
echo : echo is a language construct where there is not required to use parentheses with it and it can take any number of parameters and return void.
void echo (param1,param2,param3.....);
Example: echo "test1","test2,test3";
print : it is a language construct where there is not required to use parentheses it just take one parameter and return
1 always.
int print(param1);
print "test1";
print "test1","test2"; // It will give syntax error
prinf : It is a function which takes atleast one string and format style and returns length of output string.
int printf($string,$s);
$s= "Shailesh";
$i= printf("Hello %s how are you?",$s);
echo $i;
Output : Hello Shailesh how are you?
27
echo returns void so its execution is faster than print and printf
print_r()
is used for printing the array in human readable format.
print_r() can print out value but also if second flag parameter is passed and is TRUE - it will return printed result as string and nothing send to standard output. About var_dump. If XDebug debugger is installed so output results will be formatted in much more readable and understandable way.
they both are language constructs. echo returns void and print returns 1. echo is considered slightly faster than print.
© 2022 - 2024 — McMap. All rights reserved.