Difference in print and echo?

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

Post Reply
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

Difference in print and echo?

Post by penguinboy »

I've noticed in a lot of code people tend to use "print" for html, and "echo" for php.

so... in like....

index.php
<?
$hello = "Hello world";
print ("<table><tr><td>");
echo $hello;
print ("</td></td></table>");
?>

I was wondering if there was any logical reason for this.

A friend gave his opinon:
Print just prints, bypasses memory.
Echo goes through memory before it prints to the screen.

So according to him, echoing a var would be faster(slightly) than printing the var.
And printing html would be faster(slightly) than echoing html.

I don't know if hes right or not, so I thought I'd post the question here.


--pb
User avatar
oQEDo
Forum Commoner
Posts: 43
Joined: Thu Feb 20, 2003 11:08 am
Location: UK

Print and Echo

Post by oQEDo »

You can use print to set a return value, ie:

$myVal = print "Hello";
Therefore $myVal will be 1

echo is slightly faster as it does not do this.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

and echo 'can', $take, 'multiple arguments'; ;)
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

echo is my friend. :)
Image Image
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

echo is also one character less to type than print...

Mixing echo's and print's is a bit strange, most people tend to pick one and stick with it.

For the official word on the differences (which as people have already stated are not exactly huge):
http://www.php.net/manual/en/function.echo.php
http://www.php.net/manual/en/function.print.php

Mac
Post Reply