PHP heredoc और nowdoc
In this page:
$heredoc = <<<EOT
Text with $variable interpolated
EOT;
$nowdoc = <<<'EOT'
Text taken literally, no $variable replacement
EOT;
Heredoc को समझना
Heredoc syntax (<<<EOT ... EOT) आपको हर quote escape किए बिना एक multi-line string लिखने देता है, फिर भी अंदर variables को interpolate करते हुए exactly किसी double-quoted string जैसा।
यह PHP code में सीधे HTML का एक block या एक लंबा message embed करने के लिए handy है।
उदाहरण: Understanding Heredoc
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
$text = <<<EOT
Hello, $name!
Welcome to PHP.
EOT;
// Print `$text` to the output
echo $text;
?>
Login to try C/C++/Java/PHP code in the editor
Nowdoc को समझना
Nowdoc syntax (<<<EOT ... EOT, identifier के आसपास single quotes ध्यान दें) heredoc जैसा काम करता है लेकिन कभी variables interpolate नहीं करता, exactly कई lines में फैले एक single-quoted string जैसा behave करते हुए।
जब आपके multi-line text में literal $ characters हों जिन्हें आप PHP से expand नहीं करवाना चाहते तो इसे इस्तेमाल करें।
उदाहरण: Understanding Nowdoc
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
$text = <<<'EOT'
Hello, $name!
This is literal, not interpolated.
EOT;
// Print `$text` to the output
echo $text;
?>
Login to try C/C++/Java/PHP code in the editor
Multiline Block Generation
पुराने PHP versions में closing identifier को अपनी खुद की line की शुरुआत में शुरू होना चाहिए, हालाँकि PHP 7.3+ ने opening marker से matching indentation की अनुमति देकर इसे relax कर दिया, heredoc/nowdoc blocks को functions के अंदर neatly indented रखना आसान बनाते हुए।
उदाहरण: Multiline Block Generation
<?php
// Define the function `greet` with no parameters
function greet() {
$msg = <<<EOT
Hello!
This is indented heredoc (PHP 7.3+).
EOT;
// Return `$msg` from this function
return $msg;
}
// Print `greet()` to the output
echo greet();
?>
Login to try C/C++/Java/PHP code in the editor
Heredoc के अंदर Escaping Rules
क्योंकि heredoc interpolation support करता है, आप block के अंदर सीधे variables और यहाँ तक कि {$array[key]} जैसे simple expressions भी embed कर सकते हैं, जो dozens of string pieces को dots से जोड़ने की तुलना में templated text को readable रखता है।
उदाहरण: Escaping Rules inside Heredoc
<?php
// Declare `$data` as an array: `['key' => 'Alice']`
$data = ['key' => 'Alice'];
$text = <<<EOT
Name: {$data['key']}
EOT;
// Print `$text` to the output
echo $text;
?>
Login to try C/C++/Java/PHP code in the editor
Flexible Indentation
Heredoc और nowdoc आमतौर पर SQL queries, JSON payloads, या HTML fragments embed करने के लिए इस्तेमाल होते हैं जहाँ text लंबा है और quote characters से भरा है जिन्हें अन्यथा एक normal quoted string में लगातार escaping चाहिए होती।
उदाहरण: Flexible Indentation
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
$json = <<<EOT
{"name": "$name", "active": true}
EOT;
// Print `$json` to the output
echo $json;
?>
Login to try C/C++/Java/PHP code in the editor
- PHP 7.3 से पुराने version में closing heredoc identifier को indent करना, जो एक parse error का कारण बनता है क्योंकि इसे line की शुरुआत में शुरू होना ज़रूरी था।
- यह उम्मीद करना कि variables एक nowdoc के अंदर replace होंगे, जबकि एक nowdoc (
<<<EOT) text को literally print करता है और सिर्फ heredoc interpolate करता है। - पुराने PHP versions में closing identifier के बाद एक semicolon या दूसरा text add करना, जो heredoc को तोड़ देता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: