PHP echo और print
In this page:
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
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print `"Hello, ", $name` to the output
echo "Hello, ", $name;
?>
Login to try C/C++/Java/PHP code in the editor
print क्या है?
print echo जैसा ही basic काम करता है — output को text भेजना — लेकिन यह सिर्फ एक argument accept करता है, और एक expression के रूप में हमेशा 1 evaluate होता है।
वह return value शायद ही कभी इस्तेमाल होती है, लेकिन इसका मतलब है कि print technically एक बड़े expression के अंदर ऐसे दिख सकता है जैसे echo नहीं दिख सकता।
उदाहरण: What is print?
<?php
// Declare `$result`, set to `print "Hello"`
$result = print "Hello";
// Print `"\n" . $result` to the output
echo "\n" . $result;
?>
Login to try C/C++/Java/PHP code in the editor
कई 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
// Declare `$first`, set to "John"
$first = "John";
// Declare `$last`, set to "Doe"
$last = "Doe";
// Print `$first, ' ', $last` to the output
echo $first, ' ', $last;
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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';
?>
Login to try C/C++/Java/PHP code in the editor
echo बनाम print
Practice में echo default choice है: यह मामूली रूप से तेज़ है क्योंकि यह कुछ return नहीं करता, और यह एक call में कई arguments support करता है।
print का इकलौता edge इसकी return value है, जो शायद ही कभी वह है जो आपको असल में चाहिए, इसलिए आप जो भी ज़्यादातर PHP code पढ़ेंगे वह लगभग पूरी तरह echo को prefer करता है।
उदाहरण: echo vs print
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि
printechoजैसे कई comma-separated arguments accept करेगा;print a, b;एक parse error है। - किसी variable के साथ single quotes इस्तेमाल करना, जैसे
echo 'Hello $name';, जो$nameको इसकी value के बजाय literally print करता है। - यह भूल जाना कि
echotrueके लिए1output करता है औरfalseके लिए कुछ नहीं, इसलिए किसी boolean को echo करना confusing results देता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: