Diffrence Between Echo and 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

Post Reply
ThichHeaded
Forum Newbie
Posts: 11
Joined: Thu Dec 07, 2006 10:39 am

Diffrence Between Echo and Print??

Post by ThichHeaded »

I don't know how stupid questions can get on here but I have a feeling that I might win some type of award for it.

Anyway.

The question is this. What is the difference between echo and print?

I made a simple code to show my point.

(sorry for code below I am trying to help someone else with php altho I am limited in my knowledge.)

Code: Select all

<?php
# This is a Var test code, just showing someone how vars can be the same and diffrent..####


$Kevin = 'Kevin';
$Nkevin = 'Ed';
$nkevin = 'Maybe Kevin';

#####    Rest below is the code that is with the Variables vars here.  
#####    It isn't hard just remove the HTML coding there to see the php code itself.  


?>

<!-- Code is above here.. You Cant see it tho.. -->
<!-- PHP can not be seen in HTML format, only the Code it produces. -->

<FONT SIZE="3"><FONT color="Red"><B>Red</B></FONT> is $Kevin<BR>
<FONT color="Blue"><B>Blue</B></FONT> is $Nkevin<BR>
<FONT color="Green"><B>Green</B></FONT> is $nkevin<BR><BR></FONT>

<FONT SIZE="3">We went to meet <font color="red"><B><?php echo $Kevin;?></B></font>, but we found <font color="blue"><B><?php echo $Nkevin;?></B></font>. <font color="green"><B><?php print $nkevin;?></B></font> will be down the street.</FONT>
Now someone told me that I should never use print for some reason or another.. altho I cant remember. I always knew echo would print out whatever you had after echo whether it would be something like these below.

Code: Select all

echo $var;
or

Code: Select all

echo 'stuff here' or "stuff here";
So what is Print, In the last 3 yrs I have only come across it in one of those Dummy books. So this is farely new to me cause in most code I look at is always echo whatever.
brendandonhue
Forum Commoner
Posts: 71
Joined: Mon Sep 25, 2006 3:21 pm

Post by brendandonhue »

print() is a function that returns a value. echo is a language construct.
http://www.faqts.com/knowledge_base/vie ... d/1/fid/40
Oly
Forum Newbie
Posts: 2
Joined: Sun Dec 10, 2006 8:18 pm

Post by Oly »

they do the same thing pretty much...

BUT!

With echo you can echo multiple things within echo, IE:

Code: Select all

<?php echo $var1,$var2; ?>
and with print you could only do:

Code: Select all

<?php print $var1; print $var2; ?>
with echo also you can't do:

Code: Select all

<?php $tenary = ($var1 == $var2) ? echo 'CHEESE!' : echo 'PICKLES!'; ?>
but you can do

Code: Select all

<?php $tenary = ($var1 == $var2) ? print 'CHEESE!' : print 'PICKLES!'; ?>
or

Code: Select all

<?php $tenary = ($var1 == $var2) ? 'CHEESE!' : 'PICKLES!'; ?>
and if you ever use

Code: Select all

<?=$var1?>
it is echo'ing it with a shorter syntax.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

brendandonhue wrote:print() is a function that returns a value. echo is a language construct.
http://www.faqts.com/knowledge_base/vie ... d/1/fid/40
Both print and echo are language constructs. Echo does not return a value, and print always returns an integer '1'.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Diffrence Between Echo and Print??

Post by timvw »

ThichHeaded wrote: The question is this. What is the difference between echo and print?
The answer can be found in the manual (so please stop wasting our time and do some research yourself).
ThichHeaded
Forum Newbie
Posts: 11
Joined: Thu Dec 07, 2006 10:39 am

Post by ThichHeaded »

From reading the manual I got that they are basically the same thing. Therefor someone told me it wasn't and I read that it was. I wanted to make sure.

I wasn't asking to take up your time, I was asking because I didn't understand. Next time I will not ask simple a simple questions.

Anyway thank you for your time.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Simple questions aren't a problem. It's usually a good idea, though, to briefly describe what you had already found, and why it wasn't sufficient in answering your question.
Post Reply