This question often asked in interviews, this looks like a simple question but you know many times I failed in Interviews just because of this kind of silly questions.
So really any difference there of print and echo in PHP?
Yes, there are few worthy difference you can mention in the interviews:
1. print returns a value (the value will be 1 always), but echo won’t return anything.
For example:
1 2 3 4 5 6 7 |
<?php $res = print ""; echo $res; ?> |
The output of the above code will be 1
But for echo you cannot assign a variable, it will through a fatal error.
2. Very small speed difference between echo and print, echo works faster because it does not return value.
3. You can pass multiple parameters in echo (comma separated), but print will accept only one.
For eg:
1 2 3 4 5 |
<?php echo 1,2,"three",4,"five","<br>",6,7; ?> |
I hope this is enough to explain to the Interviewers. Enjoy the day!