Learn PHP:6-print Vs. echo

Posted: September 29, 2010 in PHP

print and echo are both used to output data to browser software or other technologies that intake external data. As you come to view different PHP scripts in your travels online you may notice some authors use echo and some use print. Let us discuss the difference.

echo() is a language construct, so you are not required to use parentheses with it. If you ever need to pass more than one parameter to echo(), the parameters must not be enclosed within parentheses.

print() behaves as a Function, but is not actually a function. print is sometimes mistaken as a function by many programmers since it sets a return value, but it is also a language construct like echo. It simply outputs a string of data. And you are not required to use parentheses with it.

OK, but talking speed which one is faster?

If you are going to be picky about miliseconds… echo is faster due to the fact that it does not set a return value like print and most functions do. So there is a difference… but not a noticable one.

Both of these methods for producing output are widely used, a matter of personal preference, and will perform virtually the same. We will use both in our different learning applications here at developPHP, but never together in the same script or application unless explicitly demonstrating an example… like the one below.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s