Page 1 of 1

echo vs print: 4 questions

Posted: Sat Oct 24, 2009 5:20 pm
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

Re: echo vs print: 4 questions

Posted: Sat Oct 24, 2009 5:55 pm
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

Re: echo vs print: 4 questions

Posted: Sat Oct 24, 2009 6:14 pm
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?

Re: echo vs print: 4 questions

Posted: Sun Oct 25, 2009 5:09 am
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 "

Re: echo vs print: 4 questions

Posted: Sun Oct 25, 2009 4:49 pm
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.

Re: echo vs print: 4 questions

Posted: Sun Oct 25, 2009 5:59 pm
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 :>