Bite of PHP: Double vs Single Quote Echo

Created: Wed, 04 Mar 2015 22:50:24 GMT

Time to read: 1 minute

In PHP, we have two ways of formatting an echo statement: we can choose to use single-quotes or double-quotes. The choice is less dependent upon which side of the Atlantic we learned to read and more dependent upon what we hope to accomplish with echoing that string.

If you want to echo the string without parsing it, use single-quotes. If you want to parse the string while echoing it, use double-quotes. Observe:

Single-Quote Example

$greeting = "Howdy";
$audience = "World";
echo '$greeting, $audience';

Output: $greeting, $audience

Double-Quote Example

$greeting = "Howdy";
$audience = "World";
echo "$greeting, $audience";

Output: Howdy, World

What if you want to add a character to the output of one of those variables? Just enclose the variable in a curly bracket!

Special Double-Quote Example

$greeting = "Howdy";
$audience = "World";
echo "$greeting, {$audience}!";

Output: Howdy, World!

Test it out on 3v4l.org.

Colophon

This site is built using Gatsby, TailwindCSS, and a whole bunch of other fun stuff.

Corrections or curious to see how this was put together? Check out the latest version of this site at its github repo.