print does almost the same thing as echo. It prints one value.
<?php
print "Hello from print";
Hello from print
The differences
echo | print | |
|---|---|---|
| Several values with commas | Yes | No, only one value |
| Returns a value | No | Yes, always 1 |
| Slightly faster | Not noticeably | Not noticeably |
| Used in real code | Very often | Rarely |
The return value
Because print returns 1, you can use it inside an expression. This is rarely useful, but it shows the difference.
<?php
$result = print "Hi\n";
echo $result;
Hi
1
echo cannot do this, because it returns nothing.
Other ways to print
PHP has more output tools. You will use them later.
printf()andsprintf()print formatted text. See Formatting Strings.print_r()andvar_dump()show the contents of variables while you debug. See Type Checking.
Which one should you use?
echo. It is the common style, it accepts several values and it is what you will see in almost all PHP code and in WordPress themes.