By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,099 Members | 2,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,099 IT Pros & Developers. It's quick & easy.

Echo vs Print

P: n/a
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 http://dynacker.dotgeek.org/printvsecho/ 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?
Jul 17 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
On Wed, 1 Jun 2005 15:11:34 -0700, "Tony" <so*****@somewhere.not>
wrote:
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 http://dynacker.dotgeek.org/printvsecho/ 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?

Hi Tony,

Heres a link about echo vs print:
http://www.faqts.com/knowledge_base/...l/aid/1/fid/40

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
Jul 17 '05 #2

P: n/a
On Wed, 1 Jun 2005 15:11:34 -0700, "Tony" <so*****@somewhere.not> wrote:
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 http://dynacker.dotgeek.org/printvsecho/ 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?


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
'print'.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #3

P: n/a
sorry for the top posting guys :S
Jul 17 '05 #4

P: n/a
> So - why does one choose "echo" over "print", or vice-versa?

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.
Jamie
Jul 17 '05 #5

P: n/a
"james" <bl**@blah.com> wrote in message
news:so********************************@fe02.buzza rdnews.com...
On Wed, 1 Jun 2005 15:11:34 -0700, "Tony" <so*****@somewhere.not>
wrote:
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 http://dynacker.dotgeek.org/printvsecho/
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?
Hi Tony,

Heres a link about echo vs print:
http://www.faqts.com/knowledge_base/...l/aid/1/fid/40


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.
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

Jul 17 '05 #6

P: n/a
"Jamie Meyers" <gt*****@mail.gatech.edu> wrote in message
news:d7**********@news-int.gatech.edu...
So - why does one choose "echo" over "print", or vice-versa?


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.


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
iteration.

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...
Jul 17 '05 #7

P: n/a
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:

<?php
if($blah == 'blah') {
print("blah = \"$blah\"");
}
?>

Jasper

Jul 17 '05 #8

P: n/a
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.

Jul 17 '05 #9

P: n/a
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
characters.

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

(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 '.
$_POST['surname_v']);
~D

Jul 17 '05 #10

P: n/a
What is the difference between that and

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

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

<?php
if(print('something'))
// makes sense, while

if(echo('something'))
// doesnt

?>

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

-
John
http://Talk-PHP.com

Jul 17 '05 #11

P: n/a
<ch***********@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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.


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 :)
Jul 17 '05 #12

P: n/a
Well, my other explanation is that we PHP programmers are all
narcissists.

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

Jul 17 '05 #13

P: n/a
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).

ECRIA
http://www.ecria.com
Jul 17 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.