PHP में echo और print में अंतर
echo एक construct है जो कई values output करता है बिना return के; print एक value लेता है और 1 return करता है. Speed का सच, comma trick और यह कब matter करता है.
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
PHP में echo और print में क्या अंतर है?
echo कई comma-separated values output करता है और कुछ return नहीं करता; print exactly एक value output करके 1 return करता है, इसलिए सिर्फ print expression के अंदर use हो सकता है. दोनों language constructs हैं.
क्या PHP में print एक function है?
नहीं — print भी echo की तरह language construct है; वह सिर्फ 1 return करने की वजह से function-जैसा behave करता है, और argument के parentheses optional हैं.
echo या print, कौन तेज़ है?
echo ज़रा-सा तेज़ है क्योंकि कुछ return नहीं करता, और commas वाला echo concatenated string बनाने से बचता है — पर असली applications में फर्क नगण्य है.