0
0
PHPprogramming~10 mins

Heredoc and nowdoc syntax in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Heredoc and nowdoc syntax
Start
Define Heredoc
Assign multiline string
Use variable inside string?
Parse variables
End Heredoc
Use string
End
Shows how heredoc and nowdoc define multiline strings, with heredoc parsing variables and nowdoc treating content literally.
Execution Sample
PHP
<?php
$name = "Anna";
$heredoc = <<<EOD
Hello, $name!
EOD;
$nowdoc = <<<'EOD'
Hello, $name!
EOD;
echo $heredoc;
echo $nowdoc;
?>
Defines a variable, then creates a heredoc string that parses the variable, and a nowdoc string that treats it literally.
Execution Table
StepActionCode LineVariable ValuesOutput/Result
1Assign $name$name = "Anna";$name = "Anna"No output
2Start heredoc assignment$heredoc = <<<EOD$heredoc = (not set yet)No output
3Assign heredoc content with variable parsedHello, $name!$heredoc = "Hello, Anna!\n"No output
4End heredocEOD;$heredoc = "Hello, Anna!\n"No output
5Start nowdoc assignment$nowdoc = <<<'EOD'$nowdoc = (not set yet)No output
6Assign nowdoc content literallyHello, $name!$nowdoc = "Hello, $name!\n"No output
7End nowdocEOD;$nowdoc = "Hello, $name!\n"No output
8Print heredocecho $heredoc;$heredoc = "Hello, Anna!\n"Hello, Anna!
9Print nowdocecho $nowdoc;$nowdoc = "Hello, $name!\n"Hello, $name!
10End script?>All variables setScript ends
💡 Script ends after printing both heredoc and nowdoc strings
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 7Final
$nameundefined"Anna""Anna""Anna""Anna"
$heredocundefinedundefined"Hello, Anna!\n""Hello, Anna!\n""Hello, Anna!\n"
$nowdocundefinedundefinedundefined"Hello, $name!\n""Hello, $name!\n"
Key Moments - 2 Insights
Why does $name get replaced inside heredoc but not inside nowdoc?
In the execution_table rows 3 and 6, heredoc parses variables like $name and replaces them with their values, while nowdoc treats everything literally, so $name stays as text.
Why do heredoc and nowdoc both end with the same marker but behave differently?
As shown in rows 4 and 7, the marker EOD ends both strings, but heredoc uses <<<EOD (without quotes) which enables variable parsing, while nowdoc uses <<<'EOD' (with single quotes) which disables parsing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of $heredoc?
A"Hello, $name!\n"
B"Hello, Anna!\n"
C"Hello, Anna!"
D"Hello, $name!"
💡 Hint
Check the 'Variable Values' column at step 3 in execution_table
At which step does $nowdoc get assigned its value?
AStep 7
BStep 6
CStep 4
DStep 2
💡 Hint
Look at when $nowdoc is set in the 'Variable Values' column in execution_table
If we change nowdoc marker to <<<EOD (without quotes), what happens to $nowdoc content?
ASyntax error occurs
BContent remains literal with variables unparsed
CVariables inside $nowdoc will be parsed
DNowdoc will be empty
💡 Hint
Refer to key_moments about difference between heredoc and nowdoc markers
Concept Snapshot
Heredoc syntax: <<<LABEL starts multiline string, variables inside are parsed.
Nowdoc syntax: <<<'LABEL' starts multiline string, content is literal, no parsing.
Both end with LABEL on a line alone.
Use heredoc for strings with variables, nowdoc for raw text.
Markers must be alone on their line with no spaces.
Full Transcript
This visual execution shows how PHP heredoc and nowdoc syntax work. We start by assigning a variable $name with value "Anna". Then we create a heredoc string using <<<EOD which allows variable parsing, so $name inside the string becomes "Anna". Next, we create a nowdoc string using <<<'EOD' which treats the content literally, so $name stays as text. Both strings end with the marker EOD on a line alone. Finally, printing the heredoc outputs "Hello, Anna!" while printing the nowdoc outputs "Hello, $name!" exactly as typed. This helps beginners see how heredoc parses variables and nowdoc does not, even though both use similar syntax markers.