TExcelTAD
Home › PHP › PHP Echo & Print

PHP Echo & Print

PHP has two language constructs for sending text to the output: echo and print. They are almost identical, and you will see echo far more often. This lesson also introduces printf() for formatted output and two debugging helpers.

echo

echo outputs one or more strings. Parentheses are not needed (it is a language construct, not a function), and several values can be separated by commas:

php · try it yourself
<?php
echo "Learning PHP is fun.\n";
echo "Multiple ", "values ", "in one echo.\n";
echo("Parentheses work too, but nobody uses them.\n");

$language = "PHP";
$year = 1995;
echo "$language first appeared in $year.\n";
echo '<p>' . $language . ' is great</p>' . "\n";
Learning PHP is fun.
Multiple values in one echo.
Parentheses work too, but nobody uses them.
PHP first appeared in 1995.
<p>PHP is great</p>

In a real web page you would usually print HTML, like the <p> tag on the last line. The browser then renders it as a paragraph.

print

print outputs exactly one string and always returns 1, so it can be used inside expressions. In practice that is rarely useful:

php · try it yourself
<?php
print "Hello from print\n";

$name = "Ana";
print "Hi $name\n";

$result = print "print returns a value: ";
echo $result, "\n";
Hello from print
Hi Ana
print returns a value: 1

echo vs print

echo print
Arguments One or more, separated by commas Exactly one
Return value None Always 1
Use inside an expression No Yes
Speed Marginally faster Marginally slower
How common Very common Rare

Tip: Just use echo. The differences almost never matter, and consistency makes code easier to read.

Short echo tag

In HTML templates, <?= $value ?> is a shortcut for <?php echo $value; ?>:

php
<h1><?= $pageTitle ?></h1>
<p>Price: <?= $price ?> EUR</p>

Formatted output with printf

printf() prints a format string in which placeholders like %s are replaced by the remaining arguments. sprintf() does the same but returns the string instead of printing it.

php · try it yourself
<?php
$item = "Notebook";
$qty = 3;
$price = 2.5;

printf("%s x %d = %.2f EUR\n", $item, $qty, $qty * $price);
printf("[%8s] [%-8s] [%05d]\n", "right", "left", 42);

$line = sprintf("%d%% done", 75);
echo $line, "\n";
Notebook x 3 = 7.50 EUR
[   right] [left    ] [00042]
75% done
Placeholder Meaning Example
%s String printf("%s", "hi") → hi
%d Integer printf("%d", 7.9) → 7
%.2f Float with 2 decimals printf("%.2f", 3.14159) → 3.14
%5s / %-5s Pad to width 5 (right / left aligned) [ hi] / [hi ]
%05d Pad with zeros 00042
%% A literal percent sign %

Debug output: print_r and var_dump

When you want to look inside a variable — especially an array — echo isn't enough. Two functions help:

  • print_r() shows a human-readable view of a value.
  • var_dump() also shows types and lengths.
php · try it yourself
<?php
$order = ["id" => 17, "paid" => true, "total" => 19.9];

print_r($order);
var_dump($order);
Array
(
    [id] => 17
    [paid] => 1
    [total] => 19.9
)
array(3) {
  ["id"]=>
  int(17)
  ["paid"]=>
  bool(true)
  ["total"]=>
  float(19.9)
}

Notice that print_r displays true as 1, while var_dump clearly says bool(true). When types matter, reach for var_dump.

Test yourself

Which statement about echo and print is true?

  1. print can take several comma-separated arguments
  2. echo returns 1
  3. print returns 1 and takes a single argument
  4. They produce different output for the same string

What does printf("%05.1f", 3.14159); print?

  1. 3.1
  2. 003.1
  3. 03.14
  4. 3.14159

Which function shows both the type and the value of a variable?

  1. echo
  2. print
  3. print_r()
  4. var_dump()