Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Single vs. Double Quotes -- confused beginner needs clarification

Status
Not open for further replies.

djbeta

IS-IT--Management
Apr 16, 2004
46
US

I'm 70 pages into my first book on PHP/MySQL (Peachpit Press, Ullman PHP/MySQL For Dynamic Web Sites)

if you can recommend a better book, please do.. not that this one is bad, but it's the only one I know!! :)

I have a question about single vs. double quotes..

so far, as far as I can tell, the only reason to use single quotes is that it might echo/print faster since it's not being interpolated... but, I find it's hard to forecast when I might need it to be interpolated (for a variable, etc.)..
so, I'm wondering..... do you think, it would be okay for me to just use double quotation marks all the time, and then use a backslash if I need to echo/print a quotation mark ??

thanks :)
 
yes, that's fine.

The difference is very simple...

In double quotes variables are expanded, and a double quote needs an escape character.

In single quotes, variables are not expanded, and a single quote needs an escape character.


Personally, that makes each one applicable in different situations for me, but there's no harm in using either one as long as you escape/concatanate appropriately.
 
I think that skiflyers response is good, as they are both good to use.

echo "hi $name";
echo 'hi ' . $name;
echo '<a href="" title="">yo</a>';
echo "<a href=\"\" title=\"\">yo</a>";

In different situations, what is easiest to use, might differ.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top