Interpolating Strings in PHP

To interpolate a string in PHP, you need to enclose the string literals in double quotes and include variables in the string, starting with $. In PHP, string interpolation only works with double-quoted strings. If single quotes are used in the string, then the variable's value is not interpolated, and the string will be printed as-is. In this PHP String Interpolation example, we use interpolation to format a string using several variables. Click Execute to run the PHP String Interpolation Example online and see the result.
Interpolating Strings in PHP Execute
<?php
$a = 'String';
$b = 'Interpolation';
$c = 'Example';
  
print_r("PHP $a $b $c");
?>
Updated: Viewed: 1784 times

What is string interpolation?

String interpolation (variable interpolation, variable substitution, or variable expansion) is the method of evaluating a string literal containing one or more placeholders, in which the placeholders are replaced with the appropriate values. In almost all languages, interpolation is preferred over concatenation for combining strings. At the same time, the string turns out to be glued, and spaces and other characters are clearly visible inside it.

PHP Interpolate Strings Example
<?php
$a = 'Hello';
$b = 'World';

print_r("$a, $b!");
?>

#output: Hello, World!

See also