PHPString Interpolation

String Interpolation

Interpolation is what makes double-quoted (and heredoc) strings useful for building dynamic text: instead of stopping the string, concatenating a variable, and starting the string again, you embed the variable right where its value should appear. PHP has two tiers of interpolation syntax — a "simple" form that covers most cases with almost no punctuation, and a "complex" curly form required the moment you're accessing a property, an array element, or a method result rather than a bare variable.

Simple syntax: bare variables

Drop a $variable directly inside a double-quoted string and PHP replaces it with its current value. This also works for a simple, single-level array access with an unquoted key.

simple-interpolation.php

PHP
<?php

$name = 'Priya';
$scores = ['math' => 92, 'art' => 88];

echo "Hello, $name!";
echo PHP_EOL;
echo "Math score: $scores[math]";
Hello, Priya!
Math score: 92
No quotes around the key in simple syntax
Inside the simple `"$scores[math]"` form, the array key is written *without* quotes — writing `"$scores['math']"` does not work the way you'd expect and produces a parse warning, because PHP interprets `'math'` as an undefined constant inside that limited syntax rather than a string literal. This inconsistency with normal PHP array syntax outside of strings is one of the most common sources of confusion for newcomers.
Complex syntax: curly braces

The moment you need to interpolate a property access (->property), a quoted array key, a static property, or the result of a chained expression, wrap the whole expression in {$...}. This tells the parser "evaluate everything inside these braces as one expression, then interpolate the result" — and it accepts full, unambiguous PHP syntax inside.

complex-interpolation.php

PHP
<?php

class User
{
    public function __construct(public string $name, public int $age) {}
}

$user = new User('Marcus', 34);
$profile = ['bio' => 'Backend developer'];

echo "User: {$user->name}, age {$user->age}.";
echo PHP_EOL;
echo "Bio: {$profile['bio']}.";
User: Marcus, age 34.
Bio: Backend developer.

Notice the array key 'bio' is quoted inside the curly form — the rule flips compared to the simple syntax, because {$...} parses its contents as genuine PHP, and genuine PHP array access always expects a quoted string key. This is exactly why many style guides recommend always using curly braces for array interpolation: one consistent rule instead of two different ones depending on complexity.

When curly braces are required, not optional
  • Object property access: "{$order->total}" — the simple form "$order->total" only works for a single, unindexed property and gets fragile fast.

  • Array access with a quoted key or a variable key: "{$items[$index]}".

  • Method or function calls: interpolating a call result, like "{$user->getFullName()}", only works inside curly braces — simple syntax cannot call anything.

  • Disambiguating a variable from following text: "{$count}Items" versus the mistake "$countItems", which PHP reads as one variable named $countItems.

disambiguation.php

PHP
<?php

$count = 3;

// Mistake: PHP looks for a variable named $countItems, which doesn't exist
echo "$countItems";
echo PHP_EOL;

// Correct: curly braces mark exactly where the variable name ends
echo "{$count}Items";

3Items
The deprecated `${name}` form

You may still encounter an older curly variant, "${name}" (dollar sign outside the braces), in legacy codebases. It behaves the same as {$name} for a plain variable, but PHP 8.2 formally deprecated it, and it will eventually be removed. New code should always use {$name} — dollar sign inside the braces — which is unambiguous and not deprecated.

deprecated-form.php

PHP
<?php

$name = 'Legacy';

// Deprecated since PHP 8.2 — avoid in new code
echo "Hello, ${name}!";

// Preferred, unambiguous, not deprecated
echo "Hello, {$name}!";
Interpolation only happens in double quotes and heredoc
Single-quoted strings and nowdoc blocks never interpolate anything — a `$variable` inside them is printed literally, character for character. If interpolation silently "isn't working," check the quote style first.
Tip
Default to the curly `{$...}` form for anything beyond a lone variable name. It costs a few extra characters but removes an entire category of "why didn't this interpolate correctly" bugs, and it reads unambiguously to the next developer regardless of how complex the expression inside becomes.