473,659 Members | 2,671 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,$strin g2,$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 23249
On Wed, 1 Jun 2005 15:11:34 -0700, "Tony" <so*****@somewh ere.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,$stri ng2,$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*****@somewh ere.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,$stri ng2,$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.andyhsoftwa re.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.co m> wrote in message
news:so******** *************** *********@fe02. buzzardnews.com ...
On Wed, 1 Jun 2005 15:11:34 -0700, "Tony" <so*****@somewh ere.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,$str ing2,$string3") , which apparently is a bit faster than
multiple
concatenation s. 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.g atech.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2169
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 refresh several times and it keeps happening. When I view page source, the last thing that shows up is the "...". The date doesn't print, nor does the closing the div tag.
6
2384
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 need to see some value. Example i have tables like this echo '<div align="',$align,'"><table border="0" cellpadding="2" cellspacing="0" width="',$largeur,'">'; echo '<tr><td bgcolor="',$titre_bgcolor,'"><font face="',$titre_face,'"...
9
4773
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 variable $noFile, it is print "1" instead of "true". Why does it do this? The loop is failing to run a second time, for reasons I can not fathom. debugNotes tells me that the count of $dirArray is 10, and the file hasn't been found yet, so I...
6
3395
by: James Smith | last post by:
What's the difference between Echo and Print? J.
25
3104
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 course then the file doesn't get seen by the user. Is there any command that essentially executes the code and then echo's it? something that will take a string like '<body>blah<?php echo 'Hello'; ?></body>' and actually interpret the php
8
3231
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' But <?php true == isset($page_seq) ? print "$page_seq" : false; ?>
2
2580
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. You migh be saying that Ms SQL can not store more than 8000 characters in a field, but in my case, it does. The Problem is when i read the data from the table and print it using "echo" or "print" etc, it doesnt display the full text. So, if the...
3
9441
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 values, but there has to be a built_in function I can call to do that for me right? Like function($months) or something? <?php $months=array('January','February','March','April'); $key=0; $calendar=count($months); while($key<=$calendar){...
3
3230
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 /proc/meminfo | grep MemFree | awk '{ print }'
0
8427
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8850
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8523
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.