Difference between echo and print

This is one of the standard interview questions but I have never got a straight answer from an prospective applicant.

There are three main differences.

Difference 1:
echo return a void, while print returns a ‘1’. Always.

Difference 2:
echo can have multiple arguments while print cannot. So, echo ‘abcd’, ‘defg’; will work. It will output ‘abcdefgh’.

Difference 3:
echo cannot be used like a function but print can even though both are language constructs. So, this will work echo strlen(print(‘abcd’)); but this won’t echo strlen(echo(‘abcd’)); You will get a parse error.

Hope in your next interview this helps you!

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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.