String Interpolation
The difference between single and double quotes, and building strings from variables.
2 min read
PHP has two everyday ways to write a string literal, and the quote character you choose isn't just style — it changes how the string is processed.
Single quotes: literal, mostly
<?php
$name = "Amara";
echo 'Hello, $name!';
// Output: Hello, $name!Inside single quotes, PHP treats the content almost entirely literally — a variable like $name is printed as-is, not replaced with its value. The only two escape sequences that still work are \\ (a literal backslash) and \' (a literal single quote).
Double quotes: interpolation
<?php
$name = "Amara";
echo "Hello, $name!";
// Output: Hello, Amara!Inside double quotes, PHP substitutes variables directly into the string — this is called interpolation. It also processes escape sequences like \n (newline) and \t (tab), which single-quoted strings do not.
Curly-brace syntax for clarity
<?php
$user = ["name" => "Amara"];
echo "Welcome, {$user['name']}!";Wrapping a variable (especially an array access or object property) in {} makes the boundary of the expression explicit, avoiding ambiguity about where the variable name ends — this matters especially when a variable is immediately followed by a letter or bracket that could otherwise be misread as part of the name.
Concatenation with the dot operator
<?php
$first = "Amara";
$last = "Diallo";
$fullName = $first . " " . $last;
echo $fullName;. joins strings together, and .= appends to an existing string variable — both work regardless of which quote style you used to create the pieces. Concatenation and interpolation solve the same problem; which one reads more clearly usually depends on how many variables are involved.
Heredoc: multi-line strings with interpolation
<?php
$name = "Amara";
$message = <<<EOT
Dear $name,
Thank you for signing up.
EOT;
echo $message;Heredoc syntax (<<<EOT ... EOT;) behaves like a double-quoted string — full interpolation — but spans multiple lines without needing \n or string concatenation, which makes it a good fit for generating a longer block of text like an email body.
A practical rule of thumb
Default to single quotes for strings with no variables (they're marginally faster since PHP doesn't need to scan for anything to interpolate), and switch to double quotes the moment you need a variable or an escape sequence inside the string.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.