Monday, April 9, 2012

Difference b/w echo and print 

Before summing up the difference I would like to tell you that echo and print both use for output some data . I know many of you know it very well but this is not for the pro programmer there are many other Rookie programmer so, this post is for them

 lets come to the point 

echo support comma separated value but print does not or we can say that echo can take multiple arguments separated by comma where as print can take one single argument

means if execute the command
echo $x,$y,$z;//This is valid
//print $x,$y; This is invalid
print $x; //This is valid

another advantage of using echo is :
echo return nothing thats why it is much faster than print because print return a value true /false

advantage of print is you can use it in some script where you need true or false but its all upto your need .

$var ="is" ; 
echo  "my", "name" , $var ,"furqan";


it will give an output

mynameisfurqan (yeah with out space ).

but if i will alter my statement with this

echo "my ","name ",$var." furqan"; // spaces added

then it will print    ----->    my name is furqan .

print statement does not support comma separated values how ever dot is universally used as a concatinator in php .But if we talk about the efficiency comma separated value are much faster than dot(concatenated value) because when we use (dot ) the whole string reserved some extra memory to combine  the strings or value where as comma separated value treat as a trailing output means they print order by order like if I print
$var = "name"; 

echo "my " ,$var ,"is ","furqan";
then it will execute like this

(first) my then it will print $var value i-e name then is and then furqan.
while if I use it with dot
 echo "my ".$var." is"." furqan";
 then first it will concat thewhole string in some memory
some php temp memory
temp memory ="my name is furqan"
then buffer it for display



Back to top!