Interpolating a string

PHP:
Interpolating a string

How to:

In PHP, you can interpolate strings using double quotes or heredoc syntax:

$name = "World";
echo "Hello, $name!"; // Output: Hello, World!

// Using curly braces for more complex variables
$object = new stdClass();
$object->greeting = "Hello";
echo "{$object->greeting}, $name!"; // Output: Hello, World!

// Heredoc syntax for multi-line strings
$heredoc = <<<EOT
This is a string that contains $name within it.
You can write as much as you want here.
EOT;
echo $heredoc; // Output: This is a string that contains World within it.

Note: Single quotes won’t interpolate:

echo 'Hello, $name!'; // Output: Hello, $name!

Deep Dive

Before PHP introduced interpolation, concatenation with the dot operator (.) was the way to go. For example:

echo 'Hello, ' . $name . '!';

Interpolation streamlines this by parsing the variable directly within the string.

String interpolation has been around since PHP 4, but the use of complex expressions within curly braces became more flexible with PHP 7. With these improvements, PHP made it easier to embed any variable, including object properties and array elements, within a string.

Alternatives to interpolation exist, such as using sprintf() for formatted strings or implode() for arrays. These may sometimes offer more control over string formatting, especially for localization and complex structures.

Implementation-wise, PHP looks for variables inside strings when they are in double quotes or heredoc syntax and replaces them with the variable’s value. The parser ignores the dollar sign ($) in single-quoted strings, treating it as a regular character.

See Also