Echo vs Print

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm just wondering why it seems most people use "echo" instead of "print". I
tend to use "print", probably because I started programming in BASIC back in
78, so it's just familiar.

Echo can take multiple expressions (as in "echo
$string1,$string2,$string3"), which apparently is a bit faster than multiple
concatenations. But for basic output, there doesn't seem to be a noticeable
difference. The speed test at shows
a "5%" difference in speed, but this amounts to a negligible difference in
actual execution time on a typical script.

So - why does one choose "echo" over "print", or vice-versa?

Re: Echo vs Print

Quoted text here. Click to load it

Here is a closer to real answer and explanation why the two calls are
different.  As you know, PHP has functions, however some methods (read:
functions) which do not return anything.  This is the essential different
between echo and print.  In visual basic, it is the difference between a sub
and a function.

Print returns its success as a boolean.  Echo on the other hand does not
return anything.  This return call is the only difference I can think of off
hand.  As for speed, returning a value does cost some cpu time but it is
usually a negligible amount.  With that said, use whatever you feel like.
Personally, I use echo because it is faster, and I never check for any
return values on print statements.  If you need to, say for important
information, then print should be used.

I hope this clears things up a bit.

Re: Echo vs Print

Quoted text here. Click to load it

No clarity needed - I'm clear on the differences. Just wondering why the
choices are made.

I don't see much speed gain - if you look at the numbers on the speed test,
they saved 4 milliseconds over 50,000 iterations. That's about 0.0001 ms per

Granted, we don't know the speed of the CPU, but it would take a pretty big
page to even begin to show a noticeable speed difference. That's why I'll
probably stick with print for general usage - it's what I know...

Re: Echo vs Print

At the end of the day, it just boils down to "echo" being easier to
type than "print." Just look at the layout of your keyboard. Typing
"echo" involves pressing [e] and [c] with your left hand, then [h] and
[o] with your right. Typing "print" involves pressing [p] with your
right hand, [r] with your left, [i] and [n] with your right, then [t]
with your left.

Re: Echo vs Print

Quoted text here. Click to load it

You have to account for "muscle memory", too - I actually type "print"
faster than "echo", simply because I've been typing "print" for 27 years :)

Re: Echo vs Print

Well, my other explanation is that we PHP programmers are all

I don't know about you but I am beautiful :-)

Re: Echo vs Print


Quoted text here. Click to load it
Hi Tony,

Heres a link about echo vs print:

I personally use echo because of all the other people that use it, the
documents i've read and the code i've studied.  However, i really
don't use echo that much anymore, rather just escape php like so:

if ($blah == "blah") {
?>blah = "<?=$blah?>"<?

but thats messy, maybe i'm just lazy

Re: Echo vs Print

Quoted text here. Click to load it

Yeah - that's on the manual pages for both echo & print. Doesn't really show
a significant difference under normal usage. It does show that each is
better suited to certain specific functions.

Quoted text here. Click to load it

Re: Echo vs Print

That's really slow (dropping in and out of parsing mode). Also you
shouldn't have short_tags enabled, it's bad for code compatibility. The
example you gave should be:

if($blah == 'blah') {
print("blah = \"$blah\"");


Re: Echo vs Print

sorry for the top posting guys :S

Re: Echo vs Print

Quoted text here. Click to load it

 I use print, with the only reason being that I am quite fond of printf - so if
I change a line from print to printf I only need to add a character.

 Possibly with a further reason that I use Perl a bit as well, which uses

< Space: disk usage analysis tool

Re: Echo vs Print

I use print, and ~almost~ exclusively with single quotes intead of
double-quotes. The primary reason is code legibility/mantianance. I
find that having the print statement look just like any other function
call provides some uniformity to the code. I also find that using
single quotes instead of double quotes requires me to separate
variables from string literals, making the statements more clear, and
potentially preventing bugs. I also really hate escaping typable

echo "My first name is $_POST['name_v'], and my last name is

(The following is correctly color-coded by editplus, allowing me to
easily see the variables and string literals)
print('My name is '. $_POST['name_v'] .', and my last name is '.


Re: Echo vs Print

What is the difference between that and

echo 'My name is '. $_POST['name_v'] .', and my last name is '.

Actually print has another use, because it returns true on success (and
why would it even return false in normal behaviour) so:

// makes sense, while

// doesnt


I use echo, w/single quotes unless otherwise needed.


Re: Echo vs Print

Why one may use "echo" instead of "print":

It's a semantic choice - "print" implies that something will be printed.
Since nothing is printed, but rather strings are added to a stream (the HTML
'file'), it is more understandable to use a term that reflects the actual
process, such as "echo" - which is more symbolic of the event - the process
of sending something back to the client making the request.

Recall that the original use of the "print" statement was to do just that -
to physically print some output. BASIC was originally developed at Dartmouth
College in 1964. The first monochrome CTR displays were introduced in the
'70s. At the time BASIC was developed, printing was about the most advanced
output method available. Nowadays, when even CRT's are declining in use,
It's fair to say that the term "print" is semantically deprecated (in the
sense that it is referred to here).


Site Timeline