473,503 Members | 11,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?
Jul 17 '05 #1
13 23236
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
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
sorry for the top posting guys :S
Jul 17 '05 #4
> 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
"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
"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
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
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
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
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
<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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2160
by: lawrence | last post by:
Right now, over at www.monkeyclaus.org, the following script is getting to the 9th run through and dying after the line: echo "..."; You'll admit that is a strange place to die. I've hit...
6
2374
by: Marco | last post by:
I have a couple pages that have tables, whats best use echo to produce the full table with the variables or is it best to just make the table in plain html and use <?php echo ('$va'l); ?> when i...
9
4764
by: lawrence | last post by:
In the following loop, at the end, the method debugNotes() is printing out some notes for me to read, so I can figure out what is going on in my script. Where I try to print out the value of the...
6
3379
by: James Smith | last post by:
What's the difference between Echo and Print? J.
25
3085
by: Jon Slaughter | last post by:
I have some code that loads up some php/html files and does a few things to them and ultimately returns an html file with some php code in it. I then pass that file onto the user by using echo. Of...
8
3221
by: Tyno Gendo | last post by:
I have just written a line of code like this (yes, many people think this stinks bad): <?php true == isset($page_seq) ? echo "$page_seq" : false; ?> However it breaks with 'Unexpected T_ECHO'...
2
2562
by: imran0092 | last post by:
Hi everyone Problem I am re-writing a website which was created in coldfusion. All the data is stored in MS SQL tables. The contents are long, some of them are over 8000 chars in a field. ...
3
9431
tharden3
by: tharden3 | last post by:
Is there a quick way to ask to 'echo' or 'print' all elements of a numeric array? Can I specify to print "all of the keys" or "all of the values"? I wrote up 5 quick lines of code to print all of my...
3
3223
by: xtremebass | last post by:
Hi Bytes.. I have command like this.. echo "km=cat /proc/meminfo | grep MemFree | awk '{ print $2 }' " i want to redirect this output to a file or output prompt i got printed km=cat...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7296
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.