Single and double quotes in PHP

I don’t know why this took me so long, but I just discovered that in PHP, there is a difference between double and single quotes. For example, the two variables below actually behave slightly differently:

$var1 = 'string\t1';
$var2 = "string\t2";

echo $var1; would produce stringt1, whereas
echo $var2; produces string 2 — with a tab character between the ‘g’ and the ‘2’.

Taking things further, the code below:

$myvar = 'text';
echo 'variable: $myvar';
echo "\n";
echo "variable: $myvar";

would output the following:

variable: $myvar
variable: text

So it seems that single quotes will output their contents literally, but double quotes are further processed before being outputted.

No Comments

Share this post

Write Back!

Help make my blog :D

Don’t like the little monster picture next to your name? Replace it with your own by getting a Gravatar for free. It works retroactively: i.e. on any comment you’ve made in the past, on millions of blogs.