Echo v's Print
Moderator: General Moderators
-
Bennettman
- Forum Contributor
- Posts: 130
- Joined: Sat Jun 15, 2002 3:58 pm
Echo v's Print
First off is there any difference between echo and print (and any others there may be ;p)?
And if there is, which is better?
And if there is, which is better?
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
...
Print acts like a function, but Echo is a language construct. Example:
It works, but this
won't work...
Stupid example btw, ...
Code: Select all
<?php
if ( print("something")){
echo "<br/>It Works";
}
?>Code: Select all
<?php
if ( echo "something"){
echo "<br/>It Works";
}
?>Stupid example btw, ...
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
Can't use echo in a ternary operator, but you can use print.
This works
This doesn't:
illustrates the language construct VS function
This works
Code: Select all
(5 < 10 ) ? print("True") : print("No");Code: Select all
(5 < 10 ) ? echo "True" : echo "No";I prefere using "echo" what's the point in doing
I mean when are you going to use this code. I undestand that print is like function but I'd like to use echo... 
Code: Select all
<?php
if ( print("something")){
echo "<br/>It Works";
}
?>Alas while this should work:
it does not...
and Takuma, you may be right, but it's there if you need it.
Code: Select all
<?php (5 < 10 ) ? echo("True") : echo("No"); ?>and Takuma, you may be right, but it's there if you need it.
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
I noticed that even
always returned true, fiddle sticks, it IS pointless.
Code: Select all
$foo = FALSE;
if(print($foo)){
return TRUE;
}- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
echo is faster, though not my anything noticeable. The other thing is that echo allows you to do this:
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:
When it's better to do it this way:
Anyways, echo is better to use for several reasons. print() offers less in exchange for more work.
Code: Select all
<?php
echo 'Hello', $world,' how are you, ', $username;
?>phpPete, I don't know why people are doing it this way:
Code: Select all
<?php
(5 < 10 ) ? echo("True") : echo("No"); ?>
?>Code: Select all
<?php
echo (5 < 10 ) ? "True" : "No";
?>Code: Select all
<?php
echo 'Hello', $world,' how are you, ', $username;
?>Code: Select all
<?php
echo 'Hello' . $world . ' how are you, ' . $username;
?>