PHP Echo and Print
Echo and print are the two statements PHP uses to print values. Most programs in this course print with echo.
Echo builds one line from pieces
Each comma adds one more piece to the line. The pieces print together as a single line.
<?php
echo "Score: ", 9, "\n";Print takes one value
print takes the one value after it here. That value prints on its own line.
<?php
print "Hello\n";Print hands back 1
The print statement runs first and prints Hi. The value it hands back lands in $x, and the echo line prints that value.
<?php
$x = print "Hi\n";
echo $x, "\n";Example
<?php
$count = 2;
$total = 8;
echo "Receipt: ";
echo $count, " pens, ";
echo $total, " dollars\n";Key points
- echo can print several values in one statement.
- Commas separate the values in an echo statement.
- print prints one value.
- print hands back the value 1 after it prints.
- Echo and print both send values to the printed output.
- echo is a little faster than print.
In this example, $count holds 2 and $total holds 8. Commas add the words after each value inside the echo statements. The pieces form one receipt line.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
How many values can one print statement print?