diff echo& print

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ezeekart
Forum Newbie
Posts: 2
Joined: Mon Jan 23, 2012 3:58 am

diff echo& print

Post by ezeekart »

What is the difference between echo and print?





Regards,
Ezeekart Team,
Buy Mobile phone online
http://www.ezeekart.com
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: diff echo& print

Post by Celauran »

echo doesn't have a return value. print returns 1, always.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: diff echo& print

Post 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
User avatar
Tiancris
Forum Commoner
Posts: 39
Joined: Sun Jan 08, 2012 9:54 pm
Location: Mar del Plata, Argentina

Re: diff echo& print

Post 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
Post Reply