Echo and Print Statement in PHP
In PHP, Echo and Print statements are used for almost the same type of tasks, that is, the work of displaying the output is done by both. The technique of writing both is also the same like:
<!DOCTYPE html> <html> <body> <?php echo 'Rkonline | Learn Programming Language <br>'; print 'Rkonline | Learn Programming Language'; ?> </body> </html>
The result of both will be the same. Thus, the work of these two is almost the same, but the following differences are also there in the work of these two:
- Echo does not give any return value where as the print method returns the value as 0 or 1.
Example
<!DOCTYPE html> <html> <body> <?php $a = print "Rkonline | Learn Programming Language"; $a = echo "Rkonline | Learn PHP Language"; ?> </body> </html>
In this, in the first line Rkonline | Learn Programming Language will print but in the second line an error like Parse error: syntax error, unexpected token "echo" will be displayed.
After the first line, if $a is printed then 1 output will come because some value is giving output it is called 'true'. If 0 appears then it will be called false.
- echo works faster than print.
- In echo, more than one value can be printed with the help of the (comma) operator whereas an error message is displayed when doing so by print.
<!DOCTYPE html> <html> <body> <?php print 'Rkonline',' Learn Programming Language'; echo 'Rkonline ',' Learn PHP Language'; ?> </body> </html>
Syntax error, an unexpected error will be displayed in its first line while Rkonline Learn PHP Language will be printed in the second line.
PHP Echo Statement
An echo statement in PHP is used to display the output. The echo statement is not a PHP function. If you give multiple parameters in this statement like parentheses, then there is a parse error. Let's understand PHP Echo Statement with an example:
Read Also - PHP Comments
Example Of PHP Echo Statement
<?php echo "<h2>PHP is Fun!</h2>"; echo "Hello world!<br>"; echo "I'm about to learn PHP Language!<br>"; echo "This ", "string ", "was ", "made ", "with multiple parameters. When Execute Then, Show Error."; ?>
How to Display Variables in PHP Echo Statement
If you want to display the variable by using Echo statement in PHP, then for this you can write code like the below example:
<!DOCTYPE html> <html> <body> <?php //Define Variable $var1 = "Hello This is echo Statement"; $var2 = "For Displaying Variable"; echo "<h3>" . $var1 . "</h3>"; echo "<p> " . $var2 . "</p>"; ?> </body> </html>
Result:
Hello This is echo Statement
For Displaying Variable
PHP Print Statement
The print statement in PHP is used to display the output, we can use it with or without parentheses.
The print statement in PHP returns a value of 1 if it is true and 0 if it is false. Let's understand it with an example -
Read Also - PHP Syntax
Example Of PHP Print Statement
<!DOCTYPE html> <html> <body> <?php print "<h3>PHP is Fun!</h3>"; print "Hello, world!<br>"; print "I'm about to learn PHP!"; ?> </body> </html>
Result:
PHP is Fun!
Hello, world!I'm about to learn PHP!
How To Print Variables in PHP
If you want to display the variable using print statement in PHP, then for this you can write code like the below example:
<!DOCTYPE html> <html> <body> <?php $var1 = "Learn PHP"; $var2 = "Rkonline.in"; $x = 10; $y = 5; $z = 5; print "<h3>" . $var1 . "</h3>"; print "Study PHP at " . $var2 . "<br>"; print $x + $y - $z; ?> </body> </html>
Result:
Learn PHP
Study PHP at Rkonline.in
10
Difference between echo and print statement
PHP Echo Statement | PHP Print Statement |
---|---|
The echo statement in PHP works very fast. | The print statement works slowly like an echo statement. |
Echo does not give any return value. | While the print method returns the value as 0 or 1. |
In an echo statement, we can pass multiple arguments by commas. | We cannot pass multiple arguments in the print statements. |
In Echo, we can display the output of one or more Strings separated by commas. | Through Print Statement we can show only Strings. |
echo can be used with or without parentheses. | The print statement can also be used with or without parentheses. |
0 Comments