PHP: The Difference between Single and Double Quotation Marks CodeUnit 27 FEB 2009

PHPThis I didn’t actually know until just a little while ago.

Now I know n means new line, r means carriage return and t means tab, but what I couldn’t figure out is why I couldn’t get PHP to spit them out correctly in my PHP echo statements. And I was like using

and everything! ;)

The problem?

Well it turns out I didn’t know that there actually is a pretty big difference between using single and double quotation marks to denote strings in PHP! And apparently this is a pretty big issue because most self-taught guys (like myself) really kind of gloss over this point in the basics.

Using single quotes essentially tells PHP that this is a string, use it as a string. Double quotes on the other hand tells PHP to parse the string, look for possible variable matches and escaping characters (like
for example) and then obviously substitute/operate on them as required.

So needless to say, using single quotes (with concatenation in of variables if required) in your script is far quicker and more efficient than using double quotes, which really should on be used when you DO actually need to make use of escaped characters and the like.

Seriously. You learn something new every day.

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.