Difference Between echo and print in PHP
echo is a construct that outputs multiple values with no return; print takes one value and returns 1. Speed truth, comma trick and when it actually matters.
The honest one-line answer
<?php
echo "Hello World"; // works
print "Hello World"; // works - same visible result
?>
Difference 1: multiple values (the comma trick)
<?php
$name = "Aman"; $marks = 92;
echo "Student: ", $name, " | Marks: ", $marks; // commas - echo only!
// print "a", "b"; // FATAL ERROR - print takes ONE argument
print "Student: " . $name; // print must concatenate
?>
Bonus: the comma version of echo is actually a hair faster than dot-concatenation, because PHP outputs the pieces directly instead of building one big string first. On shared hosting with heavy pages, echo with commas is the clean habit.
Difference 2: print returns 1
<?php
$r = print "Hello"; // valid! $r becomes 1
echo $r;
// $x = echo "Hi"; // PARSE ERROR - echo returns nothing,
// cannot be used inside an expression
// where print's return sneaks into real code (rare but legal):
$loggedIn = false;
$loggedIn or print "Please login first";
?>
Because print returns a value, it behaves like a function inside expressions. echo cannot do this — it is pure output, no value. This single point is what the interviewer is checking.
Comparison table
| Point | echo | |
|---|---|---|
| Type | Language construct | Language construct |
| Arguments | Multiple: echo $a, $b, $c; | Exactly one |
| Return value | None | Always 1 |
| Usable in expressions | ❌ No | ✅ Yes |
| Speed | Marginally faster | Marginally slower (returns 1) |
| Parentheses | Optional: echo("hi") works | Optional: print("hi") works |
What real code uses
Practically all professional PHP — WordPress, Laravel templates, your own projects — uses echo, plus its shortcut in HTML templates:
<p>Welcome, <?= $name ?></p>
<!-- <?= ... ?> is short for <?php echo ... ?> -->
Interview line: "Both are constructs; echo takes multiple arguments and returns nothing, print takes one and returns 1, so only print can sit inside an expression. In practice, echo everywhere."
ईमानदार one-line answer
<?php
echo "Hello World"; // चलता है
print "Hello World"; // चलता है - same दिखने वाला result
?>
अंतर 1: multiple values (comma trick)
<?php
$name = "Aman"; $marks = 92;
echo "Student: ", $name, " | Marks: ", $marks; // commas - सिर्फ echo!
// print "a", "b"; // FATAL ERROR - print EK argument लेता है
print "Student: " . $name; // print को concatenate करना पड़ता है
?>
Bonus: echo का comma version dot-concatenation से हल्का-सा तेज़ है, क्योंकि PHP एक बड़ी string बनाने की बजाय टुकड़े सीधे output करता है. Shared hosting पर heavy pages में commas वाला echo अच्छी आदत है.
अंतर 2: print 1 return करता है
<?php
$r = print "Hello"; // valid! $r बन गया 1
echo $r;
// $x = echo "Hi"; // PARSE ERROR - echo कुछ return नहीं करता,
// expression के अंदर use नहीं हो सकता
// print का return असली code में कहां घुसता है (rare पर legal):
$loggedIn = false;
$loggedIn or print "Please login first";
?>
Print value return करता है, इसलिए expressions के अंदर function जैसा behave करता है. echo यह नहीं कर सकता — वह pure output है, कोई value नहीं. Interviewer यही एक point check कर रहा होता है.
Comparison table
| Point | echo | |
|---|---|---|
| Type | Language construct | Language construct |
| Arguments | कई: echo $a, $b, $c; | Exactly एक |
| Return value | कुछ नहीं | हमेशा 1 |
| Expression में use | ❌ नहीं | ✅ हां |
| Speed | ज़रा-सा तेज़ | ज़रा-सा धीमा (1 return करता है) |
| Parentheses | Optional: echo("hi") चलता है | Optional: print("hi") चलता है |
असली code क्या use करता है
लगभग सारा professional PHP — WordPress, Laravel templates, आपके अपने projects — echo use करता है, plus HTML templates में उसका shortcut:
<p>Welcome, <?= $name ?></p>
<!-- <?= ... ?> मतलब <?php echo ... ?> -->
Interview line: "दोनों constructs हैं; echo कई arguments लेता है और कुछ return नहीं करता, print एक लेता है और 1 return करता है, इसलिए सिर्फ print expression में बैठ सकता है. Practice में हर जगह echo."
Frequently Asked Questions
What is the difference between echo and print in PHP?
echo outputs multiple comma-separated values and returns nothing; print outputs exactly one value and returns 1, so only print can be used inside an expression. Both are language constructs.
Is print a function in PHP?
No — print is a language construct like echo; it only behaves function-like because it returns 1, and parentheses around its argument are optional.
Which is faster, echo or print?
echo is marginally faster since it returns nothing, and echo with commas avoids building a concatenated string — but the difference is negligible in real applications.