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
$variablesand 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
- Q: Heredoc vs nowdoc?
A: Heredoc interpolates variables; nowdoc does not—pick nowdoc for literals with many$signs. - Q: How do you safely build HTML from user input?
A: Escape withhtmlspecialchars($s, ENT_QUOTES, 'UTF-8')or use a template engine that auto-escapes.
Self-check
- When would you pick nowdoc over heredoc?
- Does
'$name'interpolate$name?