Difference between echo and print in PHP

Difference between echo and print in PHP

In PHP echo() and print() are not functions. They are language constructs and hence you are not required to use paranthesis with it. They are both used to output strings and there are minor difference between echo and print in PHP.

Syntax

void echo ( string $arg1 [, string $... ] )
int print ( string $arg )

Parameters

echo can take more than one parameter when used without parentheses (like a function call) e.g.,
echo "Hello World", "!!!", 142;

but only one parameter when used with parantheses
echo( "Hello World" );

print only takes one parameter with or without parantheses.

Return Value

echo does not return any value
print always returns 1 (integer)

In PHP, print is not a really a function but a language construct. However, it behaves like a function in that it returns a value.

Speed

echo is marginally faster compared to print as echo does not return any value.

Share this post

Leave a comment

Your email address will not be published.