3

Possible Duplicate:
Reference: Comparing PHP's print and echo

I am wondering about the practical and real advantages and reasons for using echo instead of print to write output to the screen.

In many areas of my code I have this sort of thing:

PRINT("<b>".$course_id."</b>");

I understand that print returns a value whereas echo doesn't, however i'm interested to know if there is any real reason to replace the instances in my code (and there are a few unfortunately) of print with echo?

Is it just bad practice to use print in these sort of scenarios or is there reason and benefit to avoiding it?

I am using PHP 5.3.13.

Community
  • 1
  • 1
crmpicco
  • 16,605
  • 26
  • 134
  • 210
  • From [w3schools](http://www.w3schools.com/php/func_string_print.asp): Tip: The print() function is slightly slower than echo(). –  Jun 07 '12 at 09:38
  • print returns 1 always, but its slower than echo( and echo does not return anything) – Petro Popelyshko Jun 07 '12 at 09:41
  • 1
    One more difference : echo can take more than one parameter when used without parentheses. Note that echo ($arg1,$arg2) is invalid. And print only takes one parameter. – Sanjay Jun 07 '12 at 09:44
  • they are both [language constructs](http://stackoverflow.com/questions/1180184/what-is-the-difference-between-a-language-construct-and-a-built-in-function-in) and print should be insignificantly slower than echo. [This](http://stackoverflow.com/questions/7094118/reference-comparing-phps-print-and-echo) question should cover most of the details in depth, but in short there should be no reason to go through the overhead of replacing you print with echo to get a negligible performance improvement – optimusprime619 Jun 07 '12 at 09:45

3 Answers3

6

From phpbench, echo() is faster than print(). But I think nobody will notice if your code is a few microseconds slower.

flowfree
  • 16,356
  • 12
  • 52
  • 76
  • Please help us and start with looking for duplicates just leaving a link to it in a comment under the question. Thank you for your help and keep sharing the love! – hakre Jun 07 '12 at 10:39
  • And btw, phpbench is not an authorative resource. The motivation of it's webmaster is highly questionable and scientifically it's well, idiocracy. – hakre Jun 07 '12 at 10:40
3

In contrast to 'echo' print returns a value, so

touch ($file) || print "Could not create $file";

is permitted but

touch ($file) || echo "Could not create $file";

is not.

According to Fabien Potencier 'print' uses one more opcode because it actually returns something: http://fabien.potencier.org/article/8/print-vs-echo-which-one-is-faster

So 'echo' should be a little, little bit faster than 'print'. I think most developers prefer echo to print, because using echo is a bit more flexible if you just want to render text.

You could replace the 'print'-calls with the search and replace-function of an IDE and a regex, if you desire to do so, but most of the performance bottlenecks of common applications are found elsewhere, i think.

chris
  • 1,245
  • 1
  • 10
  • 22
2

The difference is by far neglectable. There's no reason to refactor out uses of the print() function.

smassey
  • 5,875
  • 24
  • 37