Echo v's Print

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Echo v's Print

Post by Bennettman »

First off is there any difference between echo and print (and any others there may be ;p)?

And if there is, which is better?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

print is better, there was a topic on it before but i couldn't find it..

it's not dramatically better, I use echo because it is habitual, there is no performance issue or anything, but there was one thing about print that made it better under certain circumstances
User avatar
QWERTY
Forum Newbie
Posts: 20
Joined: Sat Jun 29, 2002 10:57 am
Location: Slovenia

...

Post by QWERTY »

Print acts like a function, but Echo is a language construct. Example:

Code: Select all

<?php

if ( print("something"))&#123;
		echo "<br/>It Works";
&#125;

?>
It works, but this

Code: Select all

<?php

if ( echo "something")&#123;
		echo "<br/>It Works";
&#125;

?>
won't work...

Stupid example btw, ...:D
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

did you know you can do this?

echo($var,$var2);
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

:evil: Why can't we all just use echo! :P
Image Image
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

Can't use echo in a ternary operator, but you can use print.

This works

Code: Select all

(5 &lt; 10 ) ? print("True") : print("No");
This doesn't:

Code: Select all

(5 &lt; 10 ) ?  echo "True"  :  echo "No";
illustrates the language construct VS function
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

yet...

Code: Select all

&lt;?php (5 &lt; 10 ) ?  echo("True")  :  echo("No");  ?&gt;
should work.
Image Image
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

I prefere using "echo" what's the point in doing

Code: Select all

&lt;?php
if ( print("something")){ 
      echo "&lt;br/&gt;It Works"; 
} 
?&gt;
I mean when are you going to use this code. I undestand that print is like function but I'd like to use echo... 8O
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

Alas while this should work:

Code: Select all

&lt;?php (5 &lt; 10 ) ?  echo("True")  :  echo("No");  ?&gt;
it does not...
and Takuma, you may be right, but it's there if you need it.
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

being that print BEHAVES like a function (still a language construct) it makes it slower than echo. so there is no reason to use it unless you have to.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

I noticed that even

Code: Select all

$foo = FALSE;
if(print($foo))&#123;
return TRUE;
&#125;
always returned true, fiddle sticks, it IS pointless.
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post by MattF »

If you use echo instead of print you can save yourself one character each time you echo something out......
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Yeah, echo is better than print because it is one less character to type :) . Other than that it really is down to personal preference. Either one is very unlikely to make your script noticably faster/slower.

Mac
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

echo is faster, though not my anything noticeable. The other thing is that echo allows you to do this:

Code: Select all

&lt;?php
echo 'Hello', $world,' how are you, ', $username;
?&gt;
While you may wonder why you dont' just concatenate these together, concatentation actually takes longer, with the above example, PHP doesn't need to concatenate the values, it just echo's them out. Where as with print, you have to first concatenate the variables together which adds needless overhead.

phpPete, I don't know why people are doing it this way:

Code: Select all

&lt;?php
 (5 &lt; 10 ) ?  echo("True")  :  echo("No");  ?&gt; 
?&gt;
When it's better to do it this way:

Code: Select all

&lt;?php
echo  (5 &lt; 10 ) ?  "True"  :  "No";
?&gt;
Anyways, echo is better to use for several reasons. print() offers less in exchange for more work.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Code: Select all

&lt;?php 
echo 'Hello', $world,' how are you, ', $username; 
?&gt;
I never knew you could use ,'s instead of...

Code: Select all

&lt;?php 
echo 'Hello' . $world . ' how are you, ' . $username; 
?&gt;
Image Image
Post Reply