Page 1 of 2
Echo v's Print
Posted: Sat Sep 14, 2002 3:07 pm
by Bennettman
First off is there any difference between echo and print (and any others there may be ;p)?
And if there is, which is better?
Posted: Sat Sep 14, 2002 3:34 pm
by hob_goblin
print is better, there was a topic on it before but i couldn't find it..
it's not dramatically better, I use echo because it is habitual, there is no performance issue or anything, but there was one thing about print that made it better under certain circumstances
...
Posted: Sat Sep 14, 2002 5:58 pm
by QWERTY
Print acts like a function, but Echo is a language construct. Example:
Code: Select all
<?php
if ( print("something")){
echo "<br/>It Works";
}
?>
It works, but this
Code: Select all
<?php
if ( echo "something"){
echo "<br/>It Works";
}
?>
won't work...
Stupid example btw, ...

Posted: Sat Sep 14, 2002 6:12 pm
by hob_goblin
did you know you can do this?
echo($var,$var2);
Posted: Sat Sep 14, 2002 7:37 pm
by phice

Why can't we all just use echo!

Posted: Sat Sep 14, 2002 10:22 pm
by phpPete
Can't use echo in a ternary operator, but you can use print.
This works
Code: Select all
(5 < 10 ) ? print("True") : print("No");
This doesn't:
Code: Select all
(5 < 10 ) ? echo "True" : echo "No";
illustrates the language construct VS function
Posted: Sat Sep 14, 2002 11:09 pm
by phice
yet...
Code: Select all
<?php (5 < 10 ) ? echo("True") : echo("No"); ?>
should work.
Posted: Sun Sep 15, 2002 3:05 am
by Takuma
I prefere using "echo" what's the point in doing
Code: Select all
<?php
if ( print("something")){
echo "<br/>It Works";
}
?>
I mean when are you going to use this code. I undestand that print is like function but I'd like to use echo...

Posted: Sun Sep 15, 2002 5:55 am
by phpPete
Alas while this should work:
Code: Select all
<?php (5 < 10 ) ? echo("True") : echo("No"); ?>
it does not...
and Takuma, you may be right, but it's there if you need it.
Posted: Sun Sep 15, 2002 11:42 am
by dusty
being that print BEHAVES like a function (still a language construct) it makes it slower than echo. so there is no reason to use it unless you have to.
Posted: Sun Sep 15, 2002 11:53 am
by hob_goblin
I noticed that even
Code: Select all
$foo = FALSE;
if(print($foo)){
return TRUE;
}
always returned true, fiddle sticks, it IS pointless.
Posted: Sun Sep 15, 2002 12:40 pm
by MattF
If you use echo instead of print you can save yourself one character each time you echo something out......
Posted: Mon Sep 16, 2002 2:53 am
by twigletmac
Yeah, echo is better than print because it is one less character to type

. Other than that it really is down to personal preference. Either one is very unlikely to make your script noticably faster/slower.
Mac
Posted: Mon Sep 16, 2002 6:57 am
by jason
echo is faster, though not my anything noticeable. The other thing is that echo allows you to do this:
Code: Select all
<?php
echo 'Hello', $world,' how are you, ', $username;
?>
While you may wonder why you dont' just concatenate these together, concatentation actually takes longer, with the above example, PHP doesn't need to concatenate the values, it just echo's them out. Where as with print, you have to first concatenate the variables together which adds needless overhead.
phpPete, I don't know why people are doing it this way:
Code: Select all
<?php
(5 < 10 ) ? echo("True") : echo("No"); ?>
?>
When it's better to do it this way:
Code: Select all
<?php
echo (5 < 10 ) ? "True" : "No";
?>
Anyways, echo is better to use for several reasons. print() offers less in exchange for more work.
Posted: Mon Sep 16, 2002 9:25 am
by phice
Code: Select all
<?php
echo 'Hello', $world,' how are you, ', $username;
?>
I never knew you could use ,'s instead of...
Code: Select all
<?php
echo 'Hello' . $world . ' how are you, ' . $username;
?>