PHP Variable Interpolation

by Vincy. Last modified on June 6th, 2021.

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. Reason it is called ‘complex’ is because 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 variable 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 string.

$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 
EOD; 
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 in 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 is no curly braces the variable can be misinterpreted as $namePOT. This whole word as a variable. So this curly syntax will be especially useful to add prefix or suffix to a word.

$name = "PHP";
echo "I am reading {$name}POT"; // output: I am reading PHPPOT

Comments to “PHP Variable Interpolation”

Leave a Reply to IMBA Cancel reply

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

↑ Back to Top