0
0
PHPprogramming~20 mins

Heredoc and nowdoc syntax in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Heredoc and Nowdoc Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a heredoc string with variable interpolation
What is the output of this PHP code?
PHP
<?php
$name = "Alice";
$text = <<<EOD
Hello, $name!
Welcome to PHP.
EOD;
echo $text;
AHello, Alice! Welcome to PHP.
B
Hello, $name!
Welcome to PHP.
C
Hello, Alice!
Welcome to PHP.
DSyntax error
Attempts:
2 left
💡 Hint
Heredoc strings allow variable interpolation like double quotes.
Predict Output
intermediate
2:00remaining
Output of a nowdoc string with variable inside
What will this PHP code output?
PHP
<?php
$name = "Bob";
$text = <<<'EOD'
Hello, $name!
Welcome to PHP.
EOD;
echo $text;
A
Hello, $name!
Welcome to PHP.
B
Hello, Bob!
Welcome to PHP.
CSyntax error
DHello, $name! Welcome to PHP.
Attempts:
2 left
💡 Hint
Nowdoc strings do not replace variables inside.
Predict Output
advanced
2:00remaining
Behavior of heredoc with complex variable syntax
What is the output of this PHP code?
PHP
<?php
$arr = ['fruit' => 'apple'];
$text = <<<EOD
I like {$arr['fruit']}.
EOD;
echo $text;
AI like .
BI like {$arr['fruit']}.
CSyntax error
DI like apple.
Attempts:
2 left
💡 Hint
Heredoc supports complex variable parsing with braces.
Predict Output
advanced
2:00remaining
Error caused by incorrect heredoc closing identifier
What error does this PHP code produce?
PHP
<?php
$text = <<<EOD
Hello World
Eod;
echo $text;
AOutputs: Hello World
BParse error: syntax error, unexpected identifier 'Eod'
CNotice: Undefined variable Eod
DFatal error: Uncaught Error
Attempts:
2 left
💡 Hint
Heredoc closing identifier must match exactly, including case.
🧠 Conceptual
expert
2:00remaining
Difference between heredoc and nowdoc in PHP
Which statement correctly describes the difference between heredoc and nowdoc syntax in PHP?
AHeredoc parses variables and escape sequences; nowdoc treats content as plain text without parsing.
BNowdoc parses variables and escape sequences; heredoc treats content as plain text without parsing.
CBoth heredoc and nowdoc parse variables but differ in escape sequence handling.
DNeither heredoc nor nowdoc parse variables or escape sequences.
Attempts:
2 left
💡 Hint
Think about which syntax acts like double quotes and which acts like single quotes.