Page 1 of 1

diff echo& print

Posted: Mon Jan 23, 2012 5:10 am
by ezeekart
What is the difference between echo and print?





Regards,
Ezeekart Team,
Buy Mobile phone online
http://www.ezeekart.com

Re: diff echo& print

Posted: Mon Jan 23, 2012 5:21 am
by Celauran
echo doesn't have a return value. print returns 1, always.

Re: diff echo& print

Posted: Mon Jan 23, 2012 8:58 am
by twinedev
Echo can accept multiple values, where print only takes one argument.

Code: Select all

$myname = 'Greg';
$mypassion = 'programming';

print 'Hello, my name is '.$myname.' and I love '.$mypassion;
print "Hello, my name is $myname and I love $mypassion";
echo 'Hello, my name is ',$myname,' and I love ',$mypassion;
Notice the last line actually uses comma, not periods, so it passed 4 items to echo:

Code: Select all

'Hello, my name is '
$myname
' and I love '
$mypassion
This line doesn't have the overhead that the first line does of first concatenating all four items then passing that value to print.
Also this line doesn't have the overhead of parsing a double quoted string to and then passing the final value to print.

For small work, not such a big thing, but just an example of another difference. I myself have come to using the last sample.

-Greg

Re: diff echo& print

Posted: Mon Jan 23, 2012 10:25 am
by Tiancris
Ah, the eternal question: echo or print?
I prefer to use print because I see it more "standard" or "nice" when using print_r, printf, sprintf, etc.
Just a matter of taste :D