I've read somewhere, and I don't remember where, that echo
is a more efficient way of outputting data then print
.
Why does Drupal core use print
so much? Is there any special reason behind that?
I've read somewhere, and I don't remember where, that echo
is a more efficient way of outputting data then print
.
Why does Drupal core use print
so much? Is there any special reason behind that?
echo
is only faster in academic environments. The difference is very marginal. A quick benchmark I just did: The time fetching one row from a table with more than 2000 nodes is 1 unit.
In order to get an improvement of 1 unit, I would need to replace about 3.700.000 (over 3 milion) print
statements with echo
. In other words, if you want performance improvements, this is hardly the place to start.
Seen that print
can be used in a much wider range of cases then echo
, the first has some advantages. It leaves you three options:
print
echo
where possible and fallback on print
echo
always and refactor your code if echo
cannot be used, to use itDrupal has defaulted on the first.
To answer your actual question as to why this was chosen: It was not. It simply grew this way.
if (! print some_method() ) {print some_other_method()}
. Since print returns a TRUE if it actually printed something. Echo returns nothing, which is why it is faster too. Another example you will see is return print $somevalue
at the end of a method to return the status.