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: 6636 times

What is PHP?

PHP is a general-purpose server-side language for web development. PHP integrates with web development technologies such as HTML, CSS, JavaScript, and SQL. Due to its open nature and huge developer community, PHP has many libraries and frameworks that simplify and speed up web application development. PHP is also a cross-platform language, allowing it to run on various operating systems such as Linux, Windows, macOS, and Unix.

What is a string in PHP?

In PHP, a string consists of a sequence of characters, each represented by one byte. Due to the fact that a byte can only accommodate 256 characters, PHP does not natively support Unicode strings. To create a string in PHP, you have the option of utilizing single quotes ('...'), double-quoted strings ("..."), or Heredoc syntax (<<<). PHP offers a variety of built-in functions for manipulating strings, including splitting, replacing, concatenating, and comparing. Furthermore, you can easily convert strings to lowercase, convert string to int, convert string to array, and determine the length of a string.

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!

PHP Interpolate Strings Examples

The following are examples of string interpolation in PHP:

String interpolation with variables

The following is an example of string interpolation when the specified variable is substituted at the desired location in the string:

PHP String Interpolation Example
<?php
$name = "Leo";

echo "Hello, $name!";
?>

#output: Hello, Leo!

Multiple string interpolation

The following is an example of interpolating multiple strings:

PHP Multiple Strings Interpolation Example
<?php
$str1 = "String";
$str2 = "Interpolation";

echo "PHP {$str1} {$str2} Example";
?>

#output: PHP String Interpolation Example

Interpolating strings with escape characters

The following is an example of string interpolation with escape characters:

PHP Interpolating Strings with Escape Characters Example
<?php
$str = "Interpolation";
$in_str = "PHP Example: \"String  $str\"";

echo $in_str;
?>

#output: PHP Example: "String  Interpolation"

Interpolating strings with arrays

The following is an example of string interpolation with an array:

PHP Interpolation Strings with Arrays
<?php
$arr = array("apple", "banana", "orange");

$str = "I like {$arr[0]}, {$arr[1]}, and {$arr[2]}.";

echo $str; 
?>

#output: I like apple, banana, and orange.

See also