← Back to PHP Course | Chapter 6: Strings | Lesson 6 of 8

PHP heredoc और nowdoc

Heredoc और nowdoc आपको quotes की चिंता किए बिना लंबा, कई lines का text लिखने देते हैं, किसी बड़े page पर पूरा paragraph लिखने जैसा। Heredoc variables भरता है, जबकि nowdoc हर चीज़ exactly वैसे ही रखता है जैसे लिखी गई।
Syntax
php
$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
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
$text = <<<EOT
Hello, $name!
Welcome to PHP.
EOT;
// Print `$text` to the output
echo $text;
?>

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
<?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;
?>

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
<?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();
?>

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
<?php
// Declare `$data` as an array: `['key' => 'Alice']`
$data = ['key' => 'Alice'];
$text = <<<EOT
Name: {$data['key']}
EOT;
// Print `$text` to the output
echo $text;
?>

Flexible Indentation

Heredoc और nowdoc आमतौर पर SQL queries, JSON payloads, या HTML fragments embed करने के लिए इस्तेमाल होते हैं जहाँ text लंबा है और quote characters से भरा है जिन्हें अन्यथा एक normal quoted string में लगातार escaping चाहिए होती।

उदाहरण: Flexible Indentation

php
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
$json = <<<EOT
{"name": "$name", "active": true}
EOT;
// Print `$json` to the output
echo $json;
?>
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. PHP 7.3 से पुराने version में closing heredoc identifier को indent करना, जो एक parse error का कारण बनता है क्योंकि इसे line की शुरुआत में शुरू होना ज़रूरी था।
  2. यह उम्मीद करना कि variables एक nowdoc के अंदर replace होंगे, जबकि एक nowdoc (<<<EOT) text को literally print करता है और सिर्फ heredoc interpolate करता है।
  3. पुराने PHP versions में closing identifier के बाद एक semicolon या दूसरा text add करना, जो heredoc को तोड़ देता है।

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.