473,395 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 23227
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
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
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
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
by: James Smith | last post by:
What's the difference between Echo and Print? J.
25
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...

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.