echo vs print: 4 questions

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
hanna
Forum Newbie
Posts: 8
Joined: Sun Oct 18, 2009 4:53 pm

echo vs print: 4 questions

Post by hanna »

Code: Select all

<?php
 
$myVariable="Google";
 
echo '<font size="2" face="Courier">Yahoo! </ font>' .$myVariable;
 
print ("<font size=\"2\" face=\"Courier\"> Yahoo! </ font> $myVariable");
 
?>
I apologise if this seems basic in comparison to what others are asking but I'm trying to make sense of it :?

I have 4 questions:

Why use print when echo does the job & is less bloated?
Why won't print allow me to concatenate without the '.' showing up and echo does?
Why does the variable display as Courier even though it's not within the font tags?
What if I wanted the variable in a different font/colour/size etc?

Thanks
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: echo vs print: 4 questions

Post by Mark Baker »

hanna wrote:Why use print when echo does the job & is less bloated?
This comparison of the twomay answer that question
hanna wrote:Why won't print allow me to concatenate without the '.' showing up and echo does?
print will allow you to concatenate with .

Code: Select all

print('<b>'.$myVariable.'</b>');
hanna wrote:Why does the variable display as Courier even though it's not within the font tags?
It is within, because you're not closing your font tag correctly. </font> rather than </ font>. PS Use <span> or <div> with style elements rather than <font>
hanna wrote:What if I wanted the variable in a different font/colour/size etc?
Standard html. Use style elements
hanna
Forum Newbie
Posts: 8
Joined: Sun Oct 18, 2009 4:53 pm

Re: echo vs print: 4 questions

Post by hanna »

Right, thanks for that. I still have one question though, could you show me how the concatenation works with my earlier print string?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: echo vs print: 4 questions

Post by Mark Baker »

hanna wrote:Right, thanks for that. I still have one question though, could you show me how the concatenation works with my earlier print string?
Exactly the same as it does for the echo

Code: Select all

 
echo '<font size="2" face="Courier">Yahoo! </ font>' .$myVariable;
 
print ('<font size="2" face="Courier">Yahoo! </ font>' .$myVariable);
 
I suspect your problem is misunderstanding the difference between ' and "
hanna
Forum Newbie
Posts: 8
Joined: Sun Oct 18, 2009 4:53 pm

Re: echo vs print: 4 questions

Post by hanna »

There's very little difference between ' and '', but I'm following several different tutorials which gives conflicting opinions. I need to get a good book :) Thanks for the advice.
xdpirate
Forum Newbie
Posts: 6
Joined: Sat Oct 24, 2009 11:06 pm

Re: echo vs print: 4 questions

Post by xdpirate »

hanna wrote:There's very little difference between ' and '', but I'm following several different tutorials which gives conflicting opinions. I need to get a good book :) Thanks for the advice.
Basically, the difference is that you can use variables and slashed values such as \r and \n within double quotes (" "). The perk of using single quotes (' ') is that you can use the double quotes within them :>
Post Reply