How to Answer: What is the difference between echo and print in php?
Advice and answer examples written specifically for a PHP Basic Concepts job interview.
2. What is the difference between echo and print in php?
In PHP both echo and print are used to output data to a browser.
echo just outputs the value in the browser's screen while print outputs and also returns 1 when we use it.
Also, we can output multiple values using echo separated by commas.
<?php
echo "123", " 456", " some other value"; // here, we're printing multiple values using commas
echo "<br />";
$the_return_value = print "user"; // we can also do print()
echo "<Br />";
echo $the_return_value; // this is the
?>
In the above coding example, we can see that we've used echo to print out multiple values just by separating with commas but print can't do that and also print returns 1 when we use print to print something.
Technically, echo is faster than print because it doesn't return any value.
Most developers do not have a preference as the difference between echo and print are small.
Written by Sujal khatiwada on May 4th, 2021