← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

Heredoc / Nowdoc

PHP PHP 5.0+ Beginner
debt(d7/e3/b3/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints note automated detection is 'no' and tools listed (phpcs, phpstan) can flag some patterns but not reliably catch injection via interpolated user data inside heredoc — that requires code review to spot the intent behind the variable use. The indentation issue in PHP < 7.3 would be a parse error (d1), but the injection and misuse-of-interpolation issues are silent.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix indicates switching between heredoc and nowdoc or replacing string concatenation — a small localised change within one file or block. Fixing injection misuse requires adding parameterised queries, which is a pattern replacement rather than a one-liner, but stays within a single component.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). Applies broadly (web, cli, queue-worker contexts) but heredoc is a string-syntax choice that affects only the specific code blocks where it's used. It doesn't impose a gravitational pull on the rest of the codebase — each usage is independent.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap — a documented gotcha most devs eventually learn' (t5). The misconception field directly states the canonical trap: developers conflate heredoc (interpolating) and nowdoc (literal), and may unintentionally interpolate user data or fail to interpolate when they expected it. The closing-marker indentation rule in PHP < 7.3 is an additional documented gotcha.

About DEBT scoring →

Also Known As

heredoc syntax PHP heredoc nowdoc

TL;DR

Heredoc (<<<EOT) and Nowdoc (<<<'EOT') provide multi-line string syntax; Nowdoc is the single-quoted equivalent with no variable interpolation.

Explanation

Heredoc syntax starts with <<<IDENTIFIER, followed by the string on new lines, closed by IDENTIFIER on its own line. Like double-quoted strings, heredocs interpolate variables and escape sequences. Nowdoc uses <<<'IDENTIFIER' and behaves like a single-quoted string — no interpolation — making it safer for embedding large blocks of raw text, SQL, or HTML without accidental variable expansion. Both are useful for readability but can obscure injection risks if user data is embedded in a heredoc without escaping.

Common Misconception

Heredoc and nowdoc are the same syntax. Heredoc interpolates variables like a double-quoted string; nowdoc uses single-quote-style markers and treats content as a raw literal with no interpolation — similar to the difference between " and '.

Why It Matters

Heredoc syntax allows multi-line strings without escape characters — ideal for SQL queries, HTML templates, and long strings, but variable interpolation inside can introduce injection if not careful.

Common Mistakes

  • Interpolating user input directly inside heredoc SQL strings — same injection risk as regular string concatenation.
  • Not knowing that nowdoc (single-quoted heredoc) disables variable interpolation — use it when interpolation is not needed.
  • Indenting the closing marker in PHP < 7.3 — the closing marker must be at column 0 in older PHP.
  • Using heredoc for short strings where a regular quoted string is more readable.

Code Examples

✗ Vulnerable
// Variable interpolation in SQL heredoc — SQLi risk:
$id = $_GET['id'];
$sql = <<<SQL
    SELECT * FROM users WHERE id = $id
SQL;
// Use prepared statements — heredoc does not make this safe
✓ Fixed
// Heredoc — interpolates variables (like double-quoted string)
$name = 'World';
$html = <<<EOT
    <div class="greeting">
        <h1>Hello, {$name}!</h1>
    </div>
    EOT; // closing marker indentation sets the strip amount (PHP 7.3+)

// Nowdoc — no interpolation (like single-quoted string)
$template = <<<'EOT'
    Dear $name,
    Your order #$orderId is confirmed.
    EOT;
// $name and $orderId are NOT expanded — useful for SQL templates, regex, JS snippets

Added 15 Mar 2026
Edited 22 Mar 2026
Views 90
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 2 pings S 0 pings S 0 pings M
No pings yet today
No pings yesterday
Amazonbot 10 Ahrefs 7 SEMrush 7 ChatGPT 5 Bing 4 PetalBot 4 Perplexity 3 Scrapy 3 Applebot 3 Sogou 2 Unknown AI 2 Majestic 1 Meta AI 1 Twitter/X 1 Brave Search 1
crawler 50 crawler_json 3 your_contextpost 1
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
PHP php A server-side scripting language that generates web pages and APIs — the code runs on the server, and only its output (usually HTML or JSON) reaches the browser.

PHP is often the first server-side language people meet, and understanding its execution model — script starts fresh on every request, no memory between requests — explains most of how the web backend works: sessions, databases, and caching all exist to bridge that per-request amnesia.

💡 Start with PHP 8.x, declare(strict_types=1), and PDO — skip any tutorial that mentions mysql_query().

Ask Codex about PHP →
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Use heredoc for long multi-line strings that need variable interpolation, and nowdoc for literal multi-line strings — both avoid quote escaping inside the string
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Long string concatenation that heredoc would simplify; SQL query with string breaks and concatenation; heredoc used for HTML with user data without escaping
Auto-detectable: ✗ No phpcs phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Low Context: Line


✓ schema.org compliant