Heya! Could you explain to me the big differences between [print()] and [echo]. And when you'd want to use one over the other. Cheers!
| SitePoint Sponsor |
Heya! Could you explain to me the big differences between [print()] and [echo]. And when you'd want to use one over the other. Cheers!
Print returns 1 on success and 0 on failure. If you don't need to check if something printed successfully then you are wasting one byte per print. So, always use echo unless you need to check that something actually printed, though I've never encountered a situation when I had to check!
Sean![]()
Harry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
hehe, thanks agian duder. cheers!
Your welcome, again! Remember, you can always give someone positive reputation if they've helped you out - see the little green plus with an orange minus under it next to the edit button
Sean![]()
Harry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature

Also, you can specify multiple, comma-delimited, arguments with echo. E.g.So echo should win everytime. Having said that, I pretty much use print all the time [img]images/smilies/smile.gif[/img]PHP Code:<?php
$action = "Test";
echo $action, " this! ", "-End-";
?>





Originally Posted by seanf
![]()
Mike
It's not who I am underneath, but what I do that defines me.
My English ain't so good
Sean![]()
Harry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
I didn't know there was a difference between those, now I know.
torrent, i'm confused mate... could you clarify your last statement.
Here's a good thread on this topic
http://www.sitepointforums.com/showt...threadid=91258
He mean that you can pass multiple arguements to echo(), but you can't with print().
You can't do that with print().PHP Code:$var1 = "testing ";
$var2 = "echo() ";
$var3 = "now.";
echo($var1, $var2, $var3); // output: testing echo() now.
hmmmmm, so what's printf exactly?
Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.
http://www.webamoeba.co.uk

Will printPHP Code:<?php
//very basic example
$foo = "One";
$bar = "Two";
printf("Foo = %s, Bar = %s", $foo, $bar);
?>You can also format the output (for example, floating point numbers, etc). More info at ye olde:Code:Foo = One, Bar = Two
http://www.php.net/manual/en/function.printf.php
Yeah guess I could have bothered to look, lol. Is there actually any advantage of being able to do that?More info at ye olde
Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.
http://www.webamoeba.co.uk

A couple, the formatting of numbers is very useful. You might have a number like this 1.234556, but you can use printf() to display it like this 1.23, saves you having to write any code to truncate the number.
Sometimes it's more legible if you have a number of variables within a string, to show the string as:Very subjective really. Some (like me) like them, and some do not.PHP Code:printf("Error in your MySQL: Error Number [%s], Error Text [%s]\n Logged at [%s]", $errNo, $errTxt, $timestamp);
If you wish to store the resultant string to a scalar then use sprintf()





heheOriginally Posted by seanf
, why don't you make Important topic here that says this? I hardly get any
![]()
![]()
- website
Nice suggestion, seanOriginally Posted by seanf
![]()
![]()
i'm sorry, i meant your closing statement... whaddya mean by it?Originally Posted by torrent
what about this? compared to echo...
Code:function funkshun(){ $tmp = "number"; return $tmp; } $var1 = "hello"; $var2 = funkshun(); $var3 = 7; print( "$var1 ".$var2." $var3" );
Good suggestion. DoneOriginally Posted by website
I thought soOriginally Posted by redemption
Sean![]()
Harry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
Great!Originally Posted by seanf
How about the other forums? Is it possible to make a sticky that spans the Programming forums?
is everything about status nowadays? can't ya just take pride in giving good advice? :d cheers guys, this place is great!
It's not about status, it helps promote the friendly attitude of the forums
Sean![]()
Harry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
phuken eh, well i'm just playin... you know i gave you a positive when you asked earlier. how do they work anyway? i have to post so much before i can give a positive or negative?
You can give positive or negative, but the amount varies by how long you've been a member, how many posts you've made and your reputation level
Sean![]()
Harry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature

I really wouldn't spend too much time worrying about it. To be honest you use what you feel comfortable with. From a performance perspective I know of no differences between print and echo.Originally Posted by poLka
![]()
Bookmarks