Categories
Uncategorized

Print and Echo differences in PHP

Very often arises the question, what is the difference between print and echo in PHP? Understanding the difference can help you to use the most applicable construction in each case.


1) print always returns the one (1), echo returns nothing (void). As echo returns no value, it runs faster, than print (exactly on the amount of resources required to return value. The economy is small, but it is);

2) as a result, print can be used in expressions, but echo can not;

3) echo is not a function, but a language construction. Therefore, the brackets are optional. Moreover, if you want to pass multiple arguments to echo, the brackets can not be used, you just need to separate the arguments by commas;

4) print can also be used without brackets (it is also a “fake” function, it is a language construct), but you can pass only one argument;

5) echo has a shorthand: <?=$foo?>;

It follows that, in most cases, if you need to bring something to the screen or buffer, it is advantageous to use echo. Now, try to guess what the following code will print:

echo 1 . print(2), 3;

Will be displayed 2113.

Leave a Reply

Your email address will not be published. Required fields are marked *