In this tutorial, you will learn how to use PHP print statements to print any message in the web browser with the help of examples.
PHP print is similar to echo statement and can be used alternative to echo statement many times. This is also a language construct not any function. Therefore you can use print statements with or without parentheses like print
or print()
.
The main difference between PHP print and PHP echo is that PHP print can have only one argument as an input while PHP echo can have multiple.
Another difference is that PHP print always returns 1
while PHP echo does not return anything.
PHP print syntax
int print(string $arg)
Like echo statements, PHP print also used to print a string, multi-line string, escaping characters, variable value, array, etc. Below are some features of PHP print that you must know:
- PHP print is used to print a string, variable, array, etc. This is an alternative to PHP echo.
- This can be used with or without parentheses:
print
,print()
- print can accept only one argument.
- The return value of PHP print is always
1
. - This is slower than PHP echo.
PHP print: printing string
<?php print "Hello Tutorialsbook by PHP print "; print ("Tutorialsbook by PHP print()"); ?>
Output
PHP print: printing multi-line string
<?php print "PHP print by Tutorialsbook. This is the example of PHP print multi-line by PHP print statement."; ?>
Output
PHP print: print escaping character
<?php echo "PHP print by \"Tutorialsbook\""; ?>
Output
PHP print: printing variable value
<?php //defining the variables $text = "Welcome to the World of PHP!"; $num1 = 15; $num2 = 20; //echoing the variables print $text."\n"; print $num1."+".$num2."="; print $num1 + $num2; ?>
Output
15+20=35