Heredoc is a way to write long strings in PHP without using quotes. It starts with <<<IDENTIFIER and ends with the same IDENTIFIER on a new line. Variables inside heredoc are replaced with their values.
Click to reveal answer
beginner
How does nowdoc syntax differ from heredoc in PHP?
Nowdoc is like heredoc but treats the content as plain text. It uses single quotes around the identifier like <<<'IDENTIFIER'. Variables inside nowdoc are not replaced; they stay as text.
Click to reveal answer
beginner
Show the correct way to end a heredoc or nowdoc string.
The ending identifier must be on a new line, start at the beginning (no spaces), and be followed by a semicolon. For example: IDENTIFIER;
Click to reveal answer
beginner
Can you use variables inside heredoc and nowdoc? Explain.
In heredoc, variables are replaced with their values, just like in double-quoted strings. In nowdoc, variables are not replaced; they are treated as plain text.
Click to reveal answer
beginner
Why use heredoc or nowdoc instead of regular quotes?
They make it easier to write long strings or text blocks without worrying about escaping quotes. Heredoc allows variable use, nowdoc is good for raw text.
Click to reveal answer
Which syntax allows variable parsing inside the string?
ANeither heredoc nor nowdoc
BHeredoc
CBoth heredoc and nowdoc
DNowdoc
✗ Incorrect
Heredoc parses variables inside the string, replacing them with their values. Nowdoc treats everything as plain text.
How do you start a nowdoc string in PHP?
A<<<'IDENTIFIER'
B<<<"IDENTIFIER"
C<<<IDENTIFIER
D'IDENTIFIER';
✗ Incorrect
Nowdoc starts with <<< followed by the identifier in single quotes, like <<<'IDENTIFIER'.
What must be true about the ending identifier in heredoc/nowdoc?
AIt must be enclosed in quotes
BIt must be on the same line as the last string content
CIt can be indented with spaces
DIt must be at the start of a new line with no spaces
✗ Incorrect
The ending identifier must be at the start of a new line with no spaces or tabs before it.
Which of these is a benefit of using heredoc or nowdoc?
AThey allow writing multi-line strings easily
BThey automatically compress strings
CThey prevent any variable usage
DThey require escaping all quotes
✗ Incorrect
Heredoc and nowdoc let you write multi-line strings without escaping quotes.
What happens to variables inside a nowdoc string?
AThey are replaced with their values
BThey cause an error
CThey remain as plain text
DThey are converted to uppercase
✗ Incorrect
Variables inside nowdoc are not parsed and stay as plain text.
Explain how heredoc syntax works in PHP and when you might use it.
Think about writing a long message with variables inside.
You got /4 concepts.
Describe the difference between heredoc and nowdoc syntax.
One lets variables work, the other keeps text raw.
You got /4 concepts.
Practice
(1/5)
1. What is the main difference between heredoc and nowdoc syntax in PHP?
easy
A. Nowdoc parses variables inside the string, heredoc does not.
B. Neither heredoc nor nowdoc parse variables inside the string.
C. Both parse variables inside the string the same way.
D. Heredoc parses variables inside the string, nowdoc does not.
Solution
Step 1: Understand heredoc behavior
Heredoc syntax allows variables inside the string to be replaced with their values.
Step 2: Understand nowdoc behavior
Nowdoc syntax treats the string as plain text, so variables are not replaced.
Final Answer:
Heredoc parses variables inside the string, nowdoc does not. -> Option D
Quick Check:
Heredoc = variable parsing, Nowdoc = no parsing [OK]
Hint: Remember: heredoc = variables parsed, nowdoc = raw text [OK]
Common Mistakes:
Thinking nowdoc parses variables like heredoc
Confusing which syntax uses single quotes
Assuming both behave the same with variables
2. Which of the following is the correct syntax to start a nowdoc string in PHP?
easy
A. <<<'EOD'
B. <<
C. <<<"EOD"
D. <<
Solution
Step 1: Identify nowdoc syntax start
Nowdoc starts with <<< followed by a single-quoted identifier, like <<<'EOD'.
Step 2: Compare options
<<<'EOD' uses single quotes correctly; others use no quotes or double quotes which are for heredoc or invalid.
Final Answer:
<<<'EOD' -> Option A
Quick Check:
Nowdoc uses single quotes around identifier [OK]
Hint: Nowdoc always uses single quotes around the identifier [OK]
Common Mistakes:
Using double quotes or no quotes for nowdoc start
Adding semicolon after the identifier
Confusing heredoc and nowdoc start syntax
3. What will be the output of the following PHP code?
$name = "Alice";
$text = <<
medium
A. Hello, $name!
B. Hello, Alice!
C. Syntax error
D. Hello, ;
Solution
Step 1: Understand heredoc variable parsing
The heredoc syntax replaces variables like $name with their values inside the string.
Step 2: Apply variable value
$name is "Alice", so the output becomes "Hello, Alice!".
Final Answer:
Hello, Alice! -> Option B
Quick Check:
Heredoc parses variables = Hello, Alice! [OK]
Hint: Heredoc replaces variables with their values automatically [OK]
Common Mistakes:
Thinking heredoc outputs variable names literally
Confusing heredoc with nowdoc behavior
Expecting syntax error due to variable inside heredoc
4. Identify the error in this PHP code snippet using nowdoc:
$text = <<<'EOD'\
This is a nowdoc string.
EOD;
echo $text;
medium
A. Missing semicolon after nowdoc start identifier
B. Incorrect closing identifier syntax
C. Extra backslash after nowdoc start identifier
D. Syntax error due to single quotes around identifier
Solution
Step 1: Check nowdoc start syntax
Nowdoc start must be followed immediately by a newline, no extra characters like backslash after the identifier.
Step 2: Identify error in code
The code has a backslash after <<<'EOD' which is invalid syntax; it should be just <<<'EOD' followed by newline.
Final Answer:
Extra backslash after nowdoc start identifier -> Option C
Quick Check:
Nowdoc start line ends immediately after identifier, no extra chars [OK]
Hint: Nowdoc start line ends immediately after identifier, no extra chars [OK]
Common Mistakes:
Adding backslash or semicolon after nowdoc start
Using double quotes instead of single quotes
Misplacing closing identifier
5. You want to store a multi-line SQL query in a PHP variable without parsing variables inside it. Which syntax should you use and why?
$query = ???
SELECT * FROM users WHERE name = '$name';
???;
hard
A. Use nowdoc to keep the query text exactly as typed, no variable parsing.
B. Use single quotes with concatenation for variables.
C. Use double quotes with escaped variables inside the string.
D. Use heredoc to allow variable parsing for dynamic queries.
Solution
Step 1: Understand requirement
The query should keep variables like $name as plain text, not replaced by PHP values.
Step 2: Choose correct syntax
Nowdoc syntax keeps the string exactly as typed, so variables are not parsed, perfect for raw SQL queries.
Final Answer:
Use nowdoc to keep the query text exactly as typed, no variable parsing. -> Option A
Quick Check:
Nowdoc = raw text, no variable parsing [OK]
Hint: Use nowdoc for raw multi-line strings without variable parsing [OK]
Common Mistakes:
Using heredoc and expecting variables not to parse
Trying to escape variables inside double quotes unnecessarily
Concatenating strings instead of using nowdoc for raw text
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.