Originally posted by marcnyc
Is there a standard convention in PHP or even in HTML about the use of single quotes or double quotes?
The only difference in PHP is as Spacepeeple describes, with the exception in PHP that variables are interpolated in double-quoted strings but not in single-quoted ones. One other place where your choice makes a difference is if you're using the htmlspecialchars() function, since that needs to know if the string it returns will be going into an HTML attribute value that is single- or double-quoted.
For example, is it:
echo "hi";
or
echo 'hi';
???
Whichever takes your fancy.
...is it:
<input type="text" name="var">
or
<input type='text' name='var'>
???
Either one, but for convenience, use the one opposite to your PHP convention (to avoid all those quoting hassles).
I have even seen:
<input type=text name=var>
is this possible/correct at all????
This is bad/nonstandard. Do not do it. It only gets you into trouble later (<input type=text name=var value=some text> - broken).
A couple of other suggestions. If you're just outputting plain HTML, without doing any processing on it, don't bother doing it in PHP at all:
$some=PHP();
?>
<!-- Into HTML -->
<input type="text" name="thingy">
<?php //Back into PHP
That saves you one level of quotes. It's nice in forms, because you can just the occasional <?php echo $variable?> any time you need to.
Another saving is to use the heredoc syntax:
$string .= <<<EOF
echo "<input type='text' name='var'></form>";\r
EOF;
[/b]
which also saves you a level of quotes - it's especially good for big blocks of text that need to go into variables (instead of being output):
$capitalised_text = 'LOSING MY MIND';
$a_quoted_paragraph = <<<EOP
So far so good... but in many cases the variable var will contain single quotes and double quotes and I am literally $capitalised_text with multiple, nested str_replace to make sure the output does not interfere with the three levels of quotes... I am going crazy, so if you have advices or standard rules to share with me, I'd gladly learn the right way once and for all!
EOP;
With those provisos, the decision of which method to use in a given situation is a matter of style and readability.