6. PHP Echo and Print
What you will learn
- How to output text with
echoandprint - How to add line breaks and HTML
- How to inspect values with
var_dumpandprint_r - Why you must escape output
echo
echo sends text to the page. It is the most used output statement. It can take several values separated by commas.
PHP code
<?php
echo "Hello";
echo " ";
echo "World", "!", " Many", " values";
Output
Hello World! Many values
print does almost the same job but takes one value and returns 1. In practice, use echo.
PHP code
<?php
print "Printed with print";
Output
Printed with print
Outputting HTML
The output is sent to the browser, so you can include HTML tags.
PHP code
<?php
$name = "Sam";
echo "<h2>Hello, " . $name . "</h2>";
echo "<p>Welcome to the site.</p>";
What the browser shows
Hello, Sam
Welcome to the site.
Line breaks
A line break typed in PHP (\n) appears only in the page source. The browser shows it as a space. To break a line on screen, output <br> or use paragraphs.
PHP code
<?php
echo "Line one\n";
echo "Line two<br>";
echo "Line three";
What the browser shows
Line one Line two
Line three
The built-in function nl2br() turns each \n in a string into <br>.
Quotes and escaping
PHP code
<?php
echo "She said \"hi\". ";
echo 'It\'s fine. ';
echo "It's also fine.";
Output
She said “hi”. It’s fine. It’s also fine.
A backslash lets you put a quote inside a string of the same quote type.
Heredoc for long text
A heredoc holds a long block of text that can contain variables and quotes freely.
PHP code
<?php
$user = "Sam";
echo <<<HTML
<div class="card">
<h2>Hello, $user</h2>
<p>Thanks for visiting.</p>
</div>
HTML;
What the browser receives
<div class="card"> <h2>Hello, Sam</h2> <p>Thanks for visiting.</p> </div>
Inspecting values while debugging
var_dump() shows a value together with its type. print_r() prints arrays in a readable form. Wrap them in <pre> so the layout survives in the browser.
PHP code
<?php
$items = ["pen", "book"];
echo "<pre>";
var_dump(42);
var_dump("42");
var_dump(true);
print_r($items);
echo "</pre>";
What the browser shows
int(42)
string(2) "42"
bool(true)
Array
(
[0] => pen
[1] => book
)
Remove these debugging lines before a page goes live.
Escape output: a security must
If you print text that came from a visitor, such as a name typed in a form, it may contain HTML or script. Printing it raw lets an attacker inject code into your page (cross-site scripting, or XSS). Always convert special characters first with htmlspecialchars().
PHP code
<?php
$input = "<b>Sam</b>";
echo "Raw: " . $input . "<br>";
echo "Safe: " . htmlspecialchars($input, ENT_QUOTES, "UTF-8");
What the browser shows
Raw: Sam
Safe: <b>Sam</b>
The raw version ran the tags and made the text bold. The safe version shows them as plain characters. Make escaping on output a habit.
echo versus print
| echo | ||
|---|---|---|
| Values | One or several | One |
| Returns | Nothing | 1 |
| Use today | Almost always | Rarely |
Common mistakes
- Expecting
\nto make a new line on the page. - Printing visitor input without
htmlspecialchars(). - Forgetting the semicolon after
echo. - Using
var_dumpon a live page and leaving it there. - Mixing quote types so the string ends early.
Exercise
Given $comment = "I <3 PHP & HTML";, write code that prints it safely inside a paragraph.
Show solution
<?php
$comment = "I <3 PHP & HTML";
echo "<p>" . htmlspecialchars($comment, ENT_QUOTES, "UTF-8") . "</p>";
The page shows the text exactly as typed, with the symbols visible and nothing treated as HTML.
Knowledge check
1. Which statement is normally used to output text?
echo
2. Which function makes visitor text safe to print?
htmlspecialchars()
3. Which function shows a value and its type?
var_dump()
Summary
- Use
echoto output text and HTML. - Use
<br>for on-screen line breaks, andvar_dumporprint_rto debug. - Always escape outside data with
htmlspecialchars()before printing it.