Challenge - 5 Problems
Heredoc and Nowdoc Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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;
Attempts:
2 left
💡 Hint
Heredoc strings allow variable interpolation like double quotes.
✗ Incorrect
Heredoc syntax behaves like double-quoted strings, so variables inside are replaced by their values.
❓ Predict Output
intermediate2: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;
Attempts:
2 left
💡 Hint
Nowdoc strings do not replace variables inside.
✗ Incorrect
Nowdoc syntax treats the content as plain text, so variables are not replaced.
❓ Predict Output
advanced2: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;
Attempts:
2 left
💡 Hint
Heredoc supports complex variable parsing with braces.
✗ Incorrect
Using braces allows PHP to parse complex variables inside heredoc strings correctly.
❓ Predict Output
advanced2:00remaining
Error caused by incorrect heredoc closing identifier
What error does this PHP code produce?
PHP
<?php
$text = <<<EOD
Hello World
Eod;
echo $text;Attempts:
2 left
💡 Hint
Heredoc closing identifier must match exactly, including case.
✗ Incorrect
The closing identifier is case-sensitive and must match the opening one exactly, otherwise a syntax error occurs.
🧠 Conceptual
expert2:00remaining
Difference between heredoc and nowdoc in PHP
Which statement correctly describes the difference between heredoc and nowdoc syntax in PHP?
Attempts:
2 left
💡 Hint
Think about which syntax acts like double quotes and which acts like single quotes.
✗ Incorrect
Heredoc behaves like double-quoted strings, parsing variables and escape sequences. Nowdoc behaves like single-quoted strings, treating content literally.