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

PHP echo और print

echo वह तरीका है जिससे PHP visitor से बात करता है: आप इसे जो भी दें वह page पर दिख जाता है। इसे एक megaphone समझें जो आपका text या किसी variable के contents announce करता है।
Syntax
php
echo "text", $variable;   // several arguments allowed
print "text";              // one argument, returns 1

echo क्या है?

echo page पर output भेजने का workhorse है — text, किसी variable की value, या दोनों का mix।

यह एक real function के बजाय एक language construct है, इसीलिए इसे parentheses की ज़रूरत नहीं और आप इसे एक ही call में कई comma-separated values दे सकते हैं।

उदाहरण: What is echo?

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

print क्या है?

print echo जैसा ही basic काम करता है — output को text भेजना — लेकिन यह सिर्फ एक argument accept करता है, और एक expression के रूप में हमेशा 1 evaluate होता है।

वह return value शायद ही कभी इस्तेमाल होती है, लेकिन इसका मतलब है कि print technically एक बड़े expression के अंदर ऐसे दिख सकता है जैसे echo नहीं दिख सकता।

उदाहरण: What is print?

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

कई Arguments के साथ echo

echo की कई comma-separated arguments लेने की क्षमता (echo $first, ' ', $last;) इसके लिए unique है — print का कोई equivalent नहीं है।

इस तरह कई values pass करना string concatenation को पूरी तरह skip करता है, जो तब एक छोटा लेकिन real performance win है जब आप कई pieces से output बना रहे हों।

उदाहरण: 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;
?>

Variables को Output करना

Double-quoted strings automatically variables interpolate करती हैं, इसलिए echo "Hello, $name"; variable की current value inline print करता है।

Single-quoted strings interpolation skip कर देती हैं और $name को literal characters की तरह print करती हैं, जो तेज़ है लेकिन तभी उपयोगी जब आपको genuinely substitution न चाहिए।

उदाहरण: 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 बनाम print

Practice में echo default choice है: यह मामूली रूप से तेज़ है क्योंकि यह कुछ return नहीं करता, और यह एक call में कई arguments support करता है।

print का इकलौता edge इसकी return value है, जो शायद ही कभी वह है जो आपको असल में चाहिए, इसलिए आप जो भी ज़्यादातर PHP code पढ़ेंगे वह लगभग पूरी तरह echo को prefer करता है।

उदाहरण: 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. #}
आम गलतियां
  1. यह उम्मीद करना कि print echo जैसे कई comma-separated arguments accept करेगा; print a, b; एक parse error है।
  2. किसी variable के साथ single quotes इस्तेमाल करना, जैसे echo 'Hello $name';, जो $name को इसकी value के बजाय literally print करता है।
  3. यह भूल जाना कि echo true के लिए 1 output करता है और false के लिए कुछ नहीं, इसलिए किसी boolean को echo करना 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.