
July 17th, 2005, 02:28 PM
| | | Echo vs Print
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? | 
July 17th, 2005, 02:28 PM
| | | Re: Echo vs Print
On Wed, 1 Jun 2005 15:11:34 -0700, "Tony" <someone@somewhere.not>
wrote:
[color=blue]
>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?
>[/color]
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 | 
July 17th, 2005, 02:28 PM
| | | Re: Echo vs Print
On Wed, 1 Jun 2005 15:11:34 -0700, "Tony" <someone@somewhere.not> wrote:
[color=blue]
>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?[/color]
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 / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool | 
July 17th, 2005, 02:28 PM
| | | Re: Echo vs Print
sorry for the top posting guys :S | 
July 17th, 2005, 02:28 PM
| | | Re: Echo vs Print
> 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 | 
July 17th, 2005, 02:28 PM
| | | Re: Echo vs Print
"james" <blah@blah.com> wrote in message
news:socs91hnr94btm9qnvpsfb2tiolq69db0g@fe02.buzza rdnews.com...[color=blue]
> On Wed, 1 Jun 2005 15:11:34 -0700, "Tony" <someone@somewhere.not>
> wrote:
>[color=green]
>>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?
>>[/color]
> Hi Tony,
>
> Heres a link about echo vs print:
> http://www.faqts.com/knowledge_base/...l/aid/1/fid/40[/color]
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.
[color=blue]
> 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[/color] | 
July 17th, 2005, 02:28 PM
| | | Re: Echo vs Print
"Jamie Meyers" <gtg061q@mail.gatech.edu> wrote in message
news:d7lgad$26h$1@news-int.gatech.edu...[color=blue][color=green]
>> So - why does one choose "echo" over "print", or vice-versa?[/color]
>
> 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.[/color]
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... | 
July 17th, 2005, 02:28 PM
| | | 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:
<?php
if($blah == 'blah') {
print("blah = \"$blah\"");
}
?>
Jasper | 
July 17th, 2005, 02:28 PM
| | | 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. | 
July 17th, 2005, 02:28 PM
| | | 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
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 | 
July 17th, 2005, 02:28 PM
| | | Re: Echo vs Print
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 | 
July 17th, 2005, 02:28 PM
| | | Re: Echo vs Print
<chernyshevsky@hotmail.com> wrote in message
news:1117719711.603438.39110@f14g2000cwb.googlegro ups.com...[color=blue]
> 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.[/color]
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 :) | 
July 17th, 2005, 02:29 PM
| | | Re: Echo vs Print
Well, my other explanation is that we PHP programmers are all
narcissists.
I don't know about you but I am beautiful :-) | 
July 17th, 2005, 02:29 PM
| | | 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).
ECRIA http://www.ecria.com | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 205,248 network members.
|