Echo and Print Output
Output data to the browser using echo and print statements.
Echo and Print Output is a free PHP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Output in PHP
The most common ways to send output to the browser in PHP are echo and print:
echo— language construct, outputs one or more strings, no return valueprint— language construct, outputs a single string, always returns 1- Both are not functions — you don't need parentheses
Using echo
echo is the most common output method and is slightly faster than print:
<?php
echo 'Hello, World!';
echo '<br>';
echo 'PHP', ' is', ' great'; // multiple args with comma
$name = 'Alice';
echo "Hello, $name!"; // variable interpolationUsing print
print accepts only one argument and returns 1, so it can be used inside expressions:
<?php
print 'Hello from print';
print('<br>');
// print returns 1
$result = print 'hi'; // prints 'hi'
echo $result; // 1Short Echo Tag
Inside HTML templates you can use the short echo syntax <?= ?> instead of <?php echo ?>:
<!-- HTML template -->
<h1><?= $pageTitle ?></h1>
<p>Welcome, <?= htmlspecialchars($username) ?></p>
<!-- equivalent to -->
<h1><?php echo $pageTitle; ?></h1>printf and sprintf
printf outputs a formatted string; sprintf returns it instead of printing:
<?php
$price = 9.5;
printf('Price: $%.2f', $price); // Price: $9.50
$formatted = sprintf('%05d', 42); // 00042
echo $formatted;
// %s = string, %d = integer, %f = floatprint_r vs var_dump
For debugging complex values use these display functions:
print_r($val)— human-readable output, optional second arg returns the stringvar_dump($val)— includes types and lengths, great for debuggingvar_export($val)— outputs valid PHP code that recreates the value
Debugging with var_dump
Use var_dump to inspect variables during development:
<?php
$data = ['id' => 1, 'active' => true, 'score' => 9.8];
var_dump($data);
/*
array(3) {
["id"] => int(1)
["active"]=> bool(true)
["score"] => float(9.8)
}
*/Heredoc Syntax
Heredoc lets you write multi-line strings with variable interpolation without escaping quotes:
<?php
$name = 'Alice';
$html = <<<EOT
<div class="welcome">
<h1>Hello, $name!</h1>
<p>Glad you're here.</p>
</div>
EOT;
echo $html;Nowdoc Syntax
Nowdoc is like heredoc but uses single-quote delimiter — no variable parsing:
<?php
$name = 'Alice';
$text = <<<'EOT'
Hello $name — this $name will NOT be replaced.
EOT;
echo $text;
// Hello $name — this $name will NOT be replaced.Output Buffering
PHP can capture output into a buffer instead of sending it to the browser immediately:
<?php
ob_start(); // start buffering
echo 'some output';
$content = ob_get_clean(); // capture and stop
// $content = 'some output' — not yet sent to browser
$upper = strtoupper($content);
echo $upper; // SOME OUTPUTnumber_format and Locale
Format numbers for human-readable output with number_format():
<?php
$amount = 1234567.891;
echo number_format($amount); // 1,234,568
echo number_format($amount, 2); // 1,234,567.89
echo number_format($amount, 2, ',', '.'); // 1.234.567,89 (European)Quick Check
Which PHP function returns a formatted string without printing it?
Recap: PHP Output
You now know how to output data in PHP:
echofor fast, multi-argument outputprintwhen you need a return value<?= ?>short tag in templatesprintf/sprintffor formatted strings- Heredoc for multi-line interpolated strings
- Nowdoc when you don't want interpolation
- Output buffering with
ob_start()
Next: type juggling and explicit type casting.
Frequently asked questions
Is the “Echo and Print Output” lesson free?
Yes — the full text of “Echo and Print Output” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Echo and Print Output”?
Output data to the browser using echo and print statements. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Echo and Print Output” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declaring Variables in PHP
- PHP Data Types Overview
- Echo and Print Output
- Type Juggling and Type Casting