There are two types of statements in PHP by which we can generate output, one is echo and another in print.
print and echo both are used to print the data in PHP and are used frequently in the programming.
1. Echo in PHP
What is an echo in PHP: echo is used to print data in the output screen.
It can be used with or without the parenthesis.
Example:
1 2 3 4 5 | <?php echo "PHP!"; echo "I'm human being!"; echo "This ", "is ", "new ", "way ", "to learn many new things."; ?> |
Result:
PHP!
I’m human being!
This is new way to learn many new things.
1 2 3 4 5 6 7 8 9 10 | <?php $msg = "Welcome Friends"; $age = 25; $location = "India"; $param1 = 4; $param2=5; echo "<h2>" .$msg . "</h2>"; echo "I am from " . $location .", I am ".$age." year old". "<br>"; echo "Sum of two numbers are ".($param1 + $param2); ?> |
Result:
1 2 3 4 | Welcome Friends I am from India, I am 25 year old Sum of two numbers are 9 |
2. Print in PHP
What is print in PHP: print is used to print data in the output screen.
PHP echo and PHP print are doing the same work.
It can also be used with or without parenthesis.
Here we print the basic data we can also use to print object and complex data in PHP.
Example:
1 2 3 4 5 | <?php print "PHP!"; print "I'm human being!"; print "This ", "is ", "new ", "way ", "to learn many new things."; ?> |
Result:
PHP!
I’m human being!
This is new way to learn many new things.
3 Echo Vs Print in PHP
They are exactly the same working but they both have some similarities echo does not have any return value but print has a return value of 1.
And because of that print can be used in expressions.
Echo is very fast as compared to print. It can take multiple arguments but echo can take only one argument.