3

Possible Duplicate:
How are echo and print different in PHP?

As far as I know the difference between print and echo is that print returns a boolean. So whenever I use echo I can use print instead. Still, in all the code examples I'v seen until now (I'm learning PHP) they used echo. Why is that?

EDIT: Maybe the reason is that echo is faster than print (because print returns a value and echo doesn't)? Even though, I guess the speed difference is unnoticeable.

Community
  • 1
  • 1
snakile
  • 52,936
  • 62
  • 169
  • 241

6 Answers6

6

The print returns a value while echo does not making echo slightly faster (not a big deal though). You may check out this post for more:

Other than that, you use echo to output something unlike print to get some return value too. Therefore, echo gets best in the queue but nothing stops you from using print.

alt text

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • I'm not sure how we can give credit to such test. I'm persuaded it can change from versions to versions and depending of the configuration, output buffering or other factors. – Savageman Jan 14 '11 at 18:36
  • @Savageman: You are right but generally `echo` *is* slightly faster :) – Sarfraz Jan 14 '11 at 18:37
3

This article has explored this question to a greater depth than you may have even known was possible.

From: http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40

1.

  Speed. There is a difference between the two, but speed-wise it should be irrelevant which one you use. echo is marginally faster since it doesn't set a return value if you really want to get down to the nitty gritty.

2.

  Expression. print() behaves like a function in that you can do: $ret = print "Hello World"; And $ret will be 1. That means that print can be used as part of a more complex expression where echo cannot. An example from the PHP Manual:

  $b ? print "true" : print "false";

print is also part of the precedence table which it needs to be if it is to be used within a complex expression. It is just about at the bottom of the precedence list though. Only "," AND, OR and XOR are lower.

  1. Parameter(s). The grammar is: echo expression [, expression[, expression] ... ] But echo ( expression, expression ) is not valid. This would be valid: echo ("howdy"),("partner"); the same as: echo "howdy","partner"; (Putting the brackets in that simple example serves no purpose since there is no operator precedence issue with a single term like that.)

So, echo without parentheses can take multiple parameters, which get concatenated:

   echo  "and a ", 1, 2, 3;   // comma-separated without parentheses
   echo ("and a 123");        // just one parameter with parentheses

print() can only take one parameter:

   print ("and a 123");
   print  "and a 123";
Wazy
  • 8,822
  • 10
  • 53
  • 98
2

Using echo is slightly faster than print, though for most purposes it shouldn't matter.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

It doesn't change anything. You can use either one or the other. There's no particular use of 1 being returned, everyone use echo by convention, maybe it's historical. It's also faster to write (4 letters instead of 5).

Savageman
  • 9,257
  • 6
  • 40
  • 50
1

Most of the time it just comes down to personal preference.

However echo can have have more than one parameter and print returns a value.

brian_d
  • 11,190
  • 5
  • 47
  • 72
1

The short answer to your question is no, it does not matter which you use. There are minor differences, but it is nothing to worry about. I'll highlight some of them below:

  1. print can be used in expressions, while echo cannot. For example, with print, the following is possible: ($foo == true) ? print 'true' : $foo = true, but replacing with echo would cause an error
  2. echo can take multiple arguments, separated by commas, while print cannot. For example, you would do echo "hello", "world";
  3. print always "returns" the value 1
mfonda
  • 7,873
  • 1
  • 26
  • 30