PHP Variable Interpolation

by Vincy. Last modified on July 1st, 2022.

Variable interpolation is adding variables in between when specifying a string literal. PHP will parse the interpolated variables and replace the variable with its value while processing the string literal.

I PHP, a string literal can be specified in four ways,

  1. Single quoted
  2. Double quoted
  3. Heredoc syntax
  4. Nowdoc syntax
  • Interpolation: The variable parsing is allowed when the string literal is enclosed with double quotes or with heredocs.
  • Single quoted string or nowdocs, does not supports variable interpolation.

In this tutorial, we are going to see examples for interpolating a variable in a heredoc and with a string literal that is enclosed by double-quotes. This will show how it will be helpful in PHP string concatenation.

Variable interpolation syntaxes

There are two syntaxes using which the variable interpolation can be done in PHP.

  1. Simple syntax: Just put the variable within the string literal.
  2. Complex syntax: Specify the variable within curly braces. The reason it is called as ‘complex’ is that it allows to create complex string literals.

I have added an example for variable interpolation with a single word by enclosing the variable within curly braces like {$variable_name} below.

Variable interpolation in double-quoted string

The following code shows an example for interpolating variables into a string literal that is enclosed in double quotes. In this example, I have a string variable containing the value ‘PHPPOT’ which is interpolated into the echo statement.

In this program, I interpolated the variable in a single-quoted and double-quoted string. This is is to showcase that the variable interpolation in PHP works only with double-quoted strings.

$name = "PHPPOT";
echo "I am reading $name"; // output: I am reading PHPPOT
echo 'I am reading $name'; // output: I am reading $name

Variable interpolation with heredoc

The code has a heredoc statement. In this statement, I added multiple lines of string data and interpolated the $name variable into it. While printing this statement the PHP variable is parsed to print the value.

After variable parsing, the heredoc statement will be printed with the line breaks as specified.

$name = "PHPPOT";
$myDoc = <<< EOD I am reading $name
to know all about PHP
echo $myDoc;

Interpolating variable in a word (complex / curly syntax)

In this example, the $name variable is interpolated in a word. I have added the variable at the beginning of the string. We have to enclose the variable within curly brackets to interpolate them in a word.

In the below example, if there are no curly braces the variable can be misinterpreted as $namePOT. This word is a variable. So this curly syntax will be especially useful to add prefixes or suffixes to a word.

$name = "PHP";
echo "I am reading {$name}POT"; // output: I am reading PHPPOT
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Comments to “PHP Variable Interpolation”

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page