PHP Variable Interpolation

PHP supports interpolation of variables in between a set of words to be printed to the browser using PHP output statements like echo(), print() and etc. There are two methods of variable interpolation. The first method is to use variables in between string enclosed by double quotes which are shown as follows.

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

Then, the variable interpolation can also be implemented with here a document which contains several lines of string to be printed. PHP variables will be included to replace their value. A here document will be as follows to print the content as it is including line breaks, quotes and etc.

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


In another method of variable interpolation, the variables will be enclosed by curly braces to replace its value inside a word that is not separated by any white space. This can be clearly understood by the following example.

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

Here, in the above program, the variable $name is associated with the string POT without any white space. So that, the curly braces are needed around the variable name to distinguish both. Otherwise, execution process will look for the variable $namePOT, which is no more.


  • Among both of the above two methods, the first one is the easiest method.
  • Variable interpolation is not possible within single quotes

This PHP code tutorial was published on April 20, 2013.

↑ Back to Top