Skip to content
Learn Netverks

Lesson

Step 9/36 25% through track

strings-heredoc

Strings, heredoc, and nowdoc

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Strings, heredoc, and nowdoc: the syntax, APIs, and habits you need before advancing in PHP.

Teams ship Strings, heredoc, and nowdoc on every PHP codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Strings, heredoc, and nowdoc in contexts like: LAMP/LEMP stacks, Laravel apps, WordPress themes/plugins, and shared hosting.

Write PHP in the editor and click Run on server—the dev runner executes your script and returns stdout/stderr (set LEARNING_RUNNER_ENABLED=true locally).

When you can explain the previous lesson's ideas without copying starter code.

Strings hold text. Choose quote style based on whether you need variable interpolation and escape sequences.

Quote styles

  • Single quotes — literal except \' and \\
  • Double quotes — interpolates $variables and escape sequences like \n

Multiline strings

$sql = <<<SQL
SELECT id, title FROM posts
WHERE status = 'published'
SQL;

$raw = <<<'JSON'
{"debug": true}
JSON;

Heredoc (<<<LABEL) behaves like double quotes. Nowdoc (<<<'LABEL') behaves like single quotes—great for JSON or regex.

Useful functions

strlen, strtolower, trim, str_contains (PHP 8+), substr, sprintf for formatted strings.

Important interview questions and answers

  1. Q: Heredoc vs nowdoc?
    A: Heredoc interpolates variables; nowdoc does not—pick nowdoc for literals with many $ signs.
  2. Q: How do you safely build HTML from user input?
    A: Escape with htmlspecialchars($s, ENT_QUOTES, 'UTF-8') or use a template engine that auto-escapes.

Self-check

  1. When would you pick nowdoc over heredoc?
  2. Does '$name' interpolate $name?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Heredoc vs nowdoc?
  • Interpolation risk?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump