🐘 PHP Beginner

What is string interpolation in PHP?

Why Interviewers Ask This

Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid PHP basics — a prerequisite for any developer role.

Answer

PHP supports variable interpolation inside double-quoted strings — variables and expressions are automatically replaced with their values. Simple variable: "Hello, $name!". For array values or object properties, use curly braces: "User: {$user["name"]}" or "Age: {$user->age}". Single-quoted strings do NOT interpolate variables — 'Hello, $name' outputs the literal string with the dollar sign. Using single quotes for strings with no variables is marginally faster and clearly signals no interpolation is expected. The heredoc syntax (<<<EOT) also supports interpolation for multi-line strings, while nowdoc (<<<'EOT') does not.

Pro Tip

This topic has PHP-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.