Echo and Print

What is the difference between "echo" and "print" in PHP?

Questions by stan555ley

Showing Answers 1 - 15 of 15 Answers

mystxx

  • Jun 23rd, 2010
 

Echo is a bit faster than print().
Echo can take multiple expressions while print can't.
i.e. echo "Hello", "World";
Print has a return value while echo doesn't.

  Was this answer useful?  Yes

seanmullins

  • Jul 14th, 2010
 

They are actually both language constructs (printf() is a function)
The main difference is that print returns a value, so you could do something other than simply display the string. This makes it a little slower than echo.
Unless you need the return value, use echo. Plus, it's easier to type!

  Was this answer useful?  Yes

echo is the most primitive of them and just outputs the contents following the construct to the screen.

print is also a construct (so parentheses are optional when calling it) but it returns TRUE on successful output andFALSE if it was unable to print out the string.

However you can pass multiple parameters to echo like:
<?php echo 'Welcome ' 'to' ' ' 'geekInterview!'; ?>

and it will output the string "Welcome to geekInterview!"

print does not take multiple parameters.

It is also generally argued that echo is faster but usually the speed advantage is negligible and might not be there for future versions of PHP.

printf is a function not a construct and allows such advantages as formatted output but it’s the slowest way to print out data out of echo print and printf.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions