PRINT v/s ECHO :-
print1.php:-
<?php
$txt1 = "Learning PHP...!!!";
$txt2 = "print";
$x = 3;
$y = 4;
print "<h2>$txt1</h2>";
print "Implementing <b><i> $txt2 </i></b> statement<br/><br/>";
print "x = $x<br/>";
print "y = $y<br/>";
print "The result is:- x + y =";
print "<b>";
print $x + $y;
print "</b>";
?>
Screenshot:-
- In the previous posts, I have used both "print" and "echo" statements.
- So, let me discuss about their differences.
- Echo and Print are more or less the same.
- Both are used to output data to the screen.
- Echo has no return value while, print has a return value of 1 so it can be used in expressions.
- Echo can take multiple parameters while, print can take one argument.
- Echo is comparatively faster than print.
The Echo Statement:-
- The echo statement can be used with or without parentheses: "echo" or "echo()".
- It can be explained further with a suitable example:-
echo.php:-
<html>
<body>
<?php
echo "<h2>Learning PHP with Fun!</h2>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
</body>
</html>
Screenshot:-
- We can display texts and variables with echo statement.
- Consider an example:-
echo1.php:-
<?php
$txt1 = "Learning PHP...!!!";
$txt2 = "echo";
$x = 3;
$y = 4;
echo "<h2>$txt1</h2>";
echo "Implementing <b><i> $txt2 </i></b> statement<br/><br/>";
echo "x = $x<br/>";
echo "y = $y<br/>";
echo "The result is:- x + y =";
echo "<b>";
echo $x + $y;
echo "</b>";
?>
Screenshot:-
The Print Statement :-
- The print statement can be used with or without parentheses: "print" or "print()".
- Consider an example:-
print.php:-
<?php
print "<h2>Having Fun with PHP...</h2>";
print "Using Print Statement";
?>
Screenshot:-
We can display texts and variables with print statement.
Consider an example:-
print1.php:-
<?php
$txt1 = "Learning PHP...!!!";
$txt2 = "print";
$x = 3;
$y = 4;
print "<h2>$txt1</h2>";
print "Implementing <b><i> $txt2 </i></b> statement<br/><br/>";
print "x = $x<br/>";
print "y = $y<br/>";
print "The result is:- x + y =";
print "<b>";
print $x + $y;
print "</b>";
?>
Screenshot:-