PHP echo

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
Language Basics

What You’ll Learn

echo is the first output tool most PHP beginners meet. It sends text, variables, and HTML to the browser or terminal. This guide covers syntax, five practical examples, and how echo compares to print and printf().

01

Language Construct

Not a function.

02

Simple Syntax

No parentheses needed.

03

Multiple Args

Comma-separated output.

04

Variables

Print stored values.

05

HTML Output

Build web pages.

06

echo vs print

When to use each.

Definition and Usage

In PHP, echo is a language construct that outputs one or more expressions as strings. Every PHP script that shows something to a user—a greeting, a price, a table row—uses echo (or a close alternative like print) somewhere along the way.

Because echo is a construct, not a function, you write echo "Hello"; rather than echo("Hello"); (though parentheses are allowed). It has no return value; it simply sends output to the active output buffer, which ends up in the browser or on the command line.

💡
Beginner Tip

Think of echo as PHP’s show this command. Put strings in double quotes for variable expansion ("Hello, $name"), or use commas to join pieces: echo "Hello, ", $name, "!";

📝 Syntax

echo accepts one or more expressions separated by commas:

PHP
echo expression1, expression2, ...;

Parameters

  • Expressions — any number of values (strings, variables, function calls, arithmetic). PHP converts each to a string before output.

Return Value

echo returns nothing (void). You cannot assign its result to a variable.

Short Echo Tag

When short tags are enabled, <?= $variable ?> is shorthand for <?php echo $variable; ?>. Always escape user data in HTML with htmlspecialchars().

⚡ Quick Reference

Plain text
echo "Hello, PHP!";

Single string

Multiple parts
echo "Hi ", $name, "!";

Comma-separated

With concatenation
echo "Sum: " . ($a + $b);

Dot operator

Safe HTML
echo htmlspecialchars($x, ENT_QUOTES, "UTF-8");

Escape user input

Examples Gallery

These examples progress from your first echo statement to real-world patterns you will use in web apps and scripts.

📚 Getting Started

Your first output statements in PHP.

Example 1 — Output a Simple String

The classic “Hello, PHP!” program uses a single echo statement.

PHP
<?php
echo "Hello, PHP!";
?>

How It Works

PHP evaluates the string literal and sends it to the output buffer. In a browser, you see the text rendered on the page; in CLI mode (php hello.php), it prints to the terminal.

Example 2 — Multiple Comma-Separated Arguments

Pass several pieces in one statement instead of chaining many echo calls.

PHP
<?php
echo "Hello", " ", "PHP", "!";
echo "\n";
echo "Line ", 2, " ready.\n";
?>

How It Works

Each argument is converted to a string and output in order. Commas avoid extra concatenation (.) and read cleanly when joining literals, variables, and numbers.

📈 Practical Patterns

Variables, HTML, and choosing the right output tool.

Example 3 — Output Variables and Expressions

Display stored data and calculated results—the core of dynamic PHP pages.

PHP
<?php
$name  = "Mari";
$items = 3;
$price = 19.99;

echo "Customer: $name\n";
echo "Items: ", $items, "  Total: $", number_format($price * $items, 2), "\n";
echo "Discounted: $", number_format($price * $items * 0.9, 2), "\n";
?>

How It Works

Double-quoted strings expand variables ($name). You can also mix literals, variables, and function calls with commas. Use number_format() for readable currency values.

Example 4 — Output HTML Safely

Build HTML markup with echo—always escape user-supplied values.

PHP
<?php
$pageTitle = "Dashboard";
$userName  = 'Mari <admin>';

echo "<!DOCTYPE html>\n";
echo "<html lang=\"en\">\n";
echo "<head><title>", htmlspecialchars($pageTitle, ENT_QUOTES, 'UTF-8'), "</title></head>\n";
echo "<body>\n";
echo "  <h1>Welcome, ",
     htmlspecialchars($userName, ENT_QUOTES, 'UTF-8'),
     "!</h1>\n";
echo "</body>\n";
echo "</html>\n";
?>

How It Works

Trusted literals (tags, layout) can be echoed directly. Any value that came from a user or database must pass through htmlspecialchars() so characters like < display as text instead of being parsed as HTML.

Example 5 — echo vs print vs printf()

Pick the right output tool for the job.

PHP
<?php
$name = "Mari";
$score = 87;

// echo — multiple args, no return value
echo "echo: Hello, ", $name, "!\n";

// print — one expression, returns 1
$used = print "print: Score is $score\n";
echo "print returned: $used\n";

// printf — formatted placeholders
printf("printf: %s scored %d%%\n", $name, $score);
?>

How It Works

Use echo for everyday output. Use print rarely (single expression only). Reach for printf() when you need format specifiers like %s and %d for aligned or localized output.

🚀 Common Use Cases

  • Web pages — output HTML, JSON, or plain text responses from PHP scripts.
  • Debugging — quickly inspect variables during development (echo $value;).
  • CLI scripts — print status messages, progress, and reports to the terminal.
  • Dynamic content — show user names, prices, and query results inside templates.
  • API responses — send JSON strings built in PHP (echo json_encode($data);).

🧠 How echo Works

1

PHP parses the statement

The engine reads echo and collects each comma-separated expression in the statement.

Parse
2

Expressions become strings

Variables, numbers, and function return values are converted to strings automatically.

Convert
3

Output is sent

The combined text is written to the active output buffer—stdout in CLI or the HTTP response body in a web request.

Output
=

User sees the result

The browser or terminal displays the text you echoed.

📝 Notes

  • echo is a language construct, not a function—no return value.
  • Separate multiple outputs with commas, not multiple statements, when they belong together.
  • Parentheses are optional: echo("Hi"); works but echo "Hi"; is idiomatic.
  • Single-quoted strings do not expand variables: 'Hello, $name' prints literally.
  • For large HTML templates, consider mixing PHP with HTML or using a template engine instead of hundreds of echo lines.
  • Always escape user data with htmlspecialchars() before echoing into HTML.

Conclusion

echo is the simplest way to send output in PHP. Master the comma-separated syntax, variable expansion in double quotes, and safe HTML escaping—and you have the foundation for every dynamic page and script you will write.

Default to echo for general output, reach for printf() when you need formatting, and pair HTML output with htmlspecialchars() for security.

💡 Best Practices

✅ Do

  • Use echo for straightforward output of text and variables
  • Escape user input with htmlspecialchars(..., ENT_QUOTES, "UTF-8")
  • Use commas to join multiple pieces in one statement
  • Prefer double quotes when you need variable expansion
  • Remove debug echo statements before deploying to production

❌ Don’t

  • Treat echo as a function that returns a value
  • Echo raw user input inside HTML tags
  • Build huge pages from hundreds of echo lines without templates
  • Rely on echo inside SQL queries (use prepared statements)
  • Leave sensitive debug output visible to end users

Key Takeaways

Knowledge Unlocked

Five things to remember about echo

Use these points whenever you output text in PHP.

5
Core concepts
📝 02

Comma Syntax

Multiple arguments OK.

Syntax
👤 03

Variables Expand

In double quotes.

Strings
🌐 04

HTML Output

Escape user data.

Web
05

echo vs print

echo for daily use.

Compare

❓ Frequently Asked Questions

No. echo is a language construct—a built-in statement for sending output. You do not need parentheses, and you can pass multiple comma-separated arguments. It is not something you call like strlen($text).
echo expression1, expression2, ...; Each expression is converted to a string and sent to the output (browser or CLI). Example: echo "Hello, ", $name, "!";
Both output text. echo accepts multiple arguments and has no return value. print accepts only one expression and returns 1. echo is slightly faster and is the most common choice for everyday output.
Yes. echo can send HTML tags to the browser: echo "<p>Welcome</p>"; When displaying user-supplied data inside HTML, escape it first with htmlspecialchars() to prevent XSS attacks.
Yes—echo("Hello"); works because echo behaves like a function when wrapped in parentheses, but parentheses are optional and rarely used. The idiomatic style is echo "Hello";
Use echo for simple output of strings, variables, or a few joined pieces. Use printf() or sprintf() when you need formatted placeholders (%s, %d) or precise padding and alignment.
Did you know?

echo is a language construct, not a function. That is why you can pass multiple comma-separated arguments and omit parentheses—something regular PHP functions cannot do.

Continue with PHP String Functions

Now that you can output text, learn how to split, join, and escape strings with the full function reference.

String Functions →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful