Forum Moderators: coopster
My code typically ends up with a mish-mash of both - sometimes I'll use:
$tomorMonth = date('n', $tomorrow); on one line, and then,
$arrMonth = $_POST['arrive_month']; on another line (or lines)
Both seem to work okay - I don't seem to be throwing any errors - but my question here lies in the interest of getting the "best practices" usage straight so I can start implementing single or doubles as appropriate.
So... when SHOULD someone use singles over doubles ... or does it make any difference to php at all?
Neophyte
other reasons
I use double quotes when i am building queries because queries use single quotes
I use single quotes the rest of the time as I echo using the comma and never have variables resolve inside my quotes. This also helps for html as html uses double quotes
using strictly single quotes will help your syntax, arrays don't resolve inside double quotes unless you use braces around them. There are some common errors that people make when putting vars inside double quotes for queries. never having vars resolve inside strings helps you not make these common syntax errors
the double quotes are parsed by php to find where there is any variable that needs to be displayed e.g
$w = "world";
echo " Hello $w"; // taken as Hello World
echo 'Hello $w'; // taken as Hello $w
however, I have noticed that sometimes some escape characters don't work properly with single quotes.
$sql = "select mycol from mytable where mycond='" . $myvar . "'";
I use single quotes for everything else
echo '<a href="somepage.php">my link</a>';
and I use commas in my echo statements instead of concatenation because it is faster
echo '<p>this shows var1: ',$var1,'<br>and var2: ',$var2,"\n";
obviously you need to use double quotes for some things, such as making newlines resolve properly
I include it here for the other newb's like myself to benefit from:
Where I use to have to escape EVERYTHING using double quotes ...
echo "<a href=\"somepage.php\">my link</a>";
Now, as Jatar points out, it's just:
echo '<a href="somepage.php">my link</a>';
... what an unbelievable time (and debugging-hassle) saver!
I didn't know that you could use commas in an echo either - I've always concatenated my vars in an echo... but not anymore!
Thanks to all!
Neophyte