← Back to PHP Course | Chapter 2: Output & Input | Lesson 1 of 8

PHP echo & print

echo is how PHP talks to the visitor: whatever you give it shows up on the page. Think of it as a megaphone that announces your text or the contents of a variable.
Syntax
php
echo "text", $variable;   // several arguments allowed
print "text";              // one argument, returns 1

What is echo?

echo is the workhorse for sending output to the page — text, a variable's value, or a mix of both.

It's a language construct rather than a real function, which is why it doesn't need parentheses and why you can hand it several comma-separated values in a single call.

Example: What is echo?

php
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print `"Hello, ", $name` to the output
echo "Hello, ", $name;
?>

What is print?

print does the same basic job as echo — send text to the output — but it only accepts one argument, and it always evaluates to 1 as an expression.

That return value is rarely used, but it does mean print can technically appear inside a larger expression in a way echo can't.

Example: What is print?

php
<?php
// Declare `$result`, set to `print "Hello"`
$result = print "Hello";
// Print `"\n" . $result` to the output
echo "\n" . $result;
?>

echo with Multiple Arguments

echo's ability to take multiple comma-separated arguments (echo $first, ' ', $last;) is unique to it — print has no equivalent.

Passing several values this way skips string concatenation entirely, which is a small but real performance win when you're building output from many pieces.

Example: echo with Multiple Arguments

php
<?php
// Declare `$first`, set to "John"
$first = "John";
// Declare `$last`, set to "Doe"
$last = "Doe";
// Print `$first, ' ', $last` to the output
echo $first, ' ', $last;
?>

Outputting Variables

Double-quoted strings interpolate variables automatically, so echo "Hello, $name"; prints the variable's current value inline.

Single-quoted strings skip interpolation and print $name as literal characters, which is faster but only useful when you genuinely don't need substitution.

Example: Outputting Variables

php
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print "Hello, $name" to the output
echo "Hello, $name";
// Print 'Hello, $name' to the output
echo 'Hello, $name';
?>

echo vs print

In practice echo is the default choice: it's marginally faster since it returns nothing, and it supports multiple arguments in one call.

print's only edge is its return value, which is rarely what you actually need, so most PHP code you'll read favors echo almost exclusively.

Example: echo vs print

php
<?php
// Print "echo", " ", "supports multiple args" to the output
echo "echo", " ", "supports multiple args";
// Print "print only takes one" to the output
print "print only takes one";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting print to accept several comma-separated arguments like echo does; print a, b; is a parse error.
  2. Using single quotes with a variable, such as echo 'Hello $name';, which prints $name literally instead of its value.
  3. Forgetting that echo outputs 1 for true and nothing for false, so echoing a boolean gives confusing results.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.