Posts Tagged ‘PHP’

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.

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

No Comments

Share this post