PHP echo and print Statements
PHP echo and print Statements: Understanding the Similarities and Differences
In PHP, both echo and print are used to output data to the web browser.
The PHP echo Statement
The echo statement is a fundamental tool in PHP for displaying information on a web page. It's a language construct, meaning it doesn't require parentheses like a function, but parentheses can be used for clarity when dealing with multiple arguments..
Example
<!DOCTYPE html> <html> <body> <?php echo "<h2>PHP is Fun!</h2>"; echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?> </body> </html>
Output
PHP is Fun!
Hello world!
I'm about to learn PHP!
This string was made with multiple parameters.
The PHP print Statement
The print statement can be used with or without parentheses: print or print().
The following example shows how to output text with the print command (notice that the text can contain HTML markup):
Example
<!DOCTYPE html> <html> <body> <?php print "<h2>PHP is Fun!</h2>"; print "Hello world!<br>";
print "I'm about to learn PHP!<br>";
?> </body> </html>
Output
PHP is Fun!
Hello world!
I'm about to learn PHP!