The PHP print Statement
The print
statement can be used with or without parentheses: print
or print()
.
Display Text
The following example shows how to output text with the print
command (notice that the text can contain HTML markup):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Print</title> </head> <body> <?php print "<h2>PHP is Fun!</h2>"; print "Hello world!<br>"; print "I'm about to learn PHP!"; ?> </body> </html> |
Output:
PHP is Fun!
Hello world!
I’m about to learn PHP!
PHP print: printing variable value
The following example shows how to output text and variables with the print
statement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Print</title> </head> <body> <?php $txt1 = "Learn PHP"; $txt2 = "mywebcode.com"; $x = 5; $y = 4; print "<h2>" . $txt1 . "</h2>"; print "Study PHP at " . $txt2 . "<br>"; print $x + $y; ?> </body> </html> |
Output:
Learn PHP
Study PHP at mywebcode.com
9