PHP echo and print Statements
- They are both used to output data to the screen.
- echo and print are more or less the same.
PHP Echo Statement
The echo statement can be used with or without parentheses: echo or echo().
Display Text
<!DOCTYPE html>
<html>
<body>
<?php
echo "<h2>Hello Php</h2>";
echo "Hello world!<br>";
echo "learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
</body>
</html>
Output:
Hello Php
Hello world!
learn PHP!
This string was made with multiple parameters.
Display Variable
" . $txt1 . " <!DOCTYPE html>
<html>
<body>
<?php
$txt1 = "Learn PHP";
$txt2 = "and More ";
$x = 5;
$y = 4;
echo "<h2>" . $txt1 . "</h2>";
echo "Study PHP at " . $txt2 . "<br>";
echo $x + $y;
?>
</body>
</html>
Output:
Learn PHP
Study PHP at and More
9
The PHP print Statement.
Difference Between Echo and Print in PHP
echo statement | print statement |
---|---|
echo accepts a list of arguments (multiple arguments can be passed), separated by commas. | print accepts only one argument at a time. |
It returns no value or returns void. | It returns the value 1. |
It displays the outputs of one or more strings separated by commas. | The print outputs only the strings. |
It is comparatively faster than the print statement. | It is slower than an echo statement. |
0 Comments