Single And Double Quotes
Here's something that people get confused about quite alot, the use of single and double quotes in PHP. Is there a difference? yes there is, so when do we use one rather than the other, or which is best when both will work?
Differences
The major difference is that single quotes will not parse variables from the string, it's read "as is" for example:
<?php
$number = 10;
echo 'I have $number feet'
?>
This would return the text "I have $number feet" as the $number value isn't read as a variable but as plain text.
A couple of important things to remember when using single quotes is that when including another single quote within the string it needs to be escaped, for example:
<?php echo 'I\'m Crazy' ?>
And secondly, because of that, to use a backslash in a single quoted string it should be doubled, for example:
<?php echo 'The program should be installed in c:\\program files' ?>
Back to the first example, this time using double quotes. Double quotes will parse variables in a string so the output we get is different:
<?php
$number = 10;
echo "I have $number feet"
?>
This would return the output as "I have 10 feet" as the variable is read as a value not as plain text.
As before, when using additional quotes within the string itself they need to be escaped and backslashes need to be doubled:
<?php echo "The duck said \"quack\"" ?>
In addition to variables you would need to make use of double quotes when including special characters like (%) or (@) in the string or special sequences like a linefeed (\n) or carriage return (\r). This doesn't mean you can't output variables or special characters in single quoted strings, you would just need to do it a little differently, for example:
<?php
$number = 10;
echo 'I have '.$number.' feet'
?>
This would return the same result as using double quotes.
So what to use when both will work? Generally, i think the best idea is to use single quotes for static text and HTML, as the PHP parser is not looking for any variables or special characters it'll be a little quicker, and stick to using double quotes for strings containing variables, special characters, symbols and for regular expressions.
Printing Text
Quoted strings also give you the option to break an output over miltiple lines, this is useful when printing large chunks of HTML, example:
<?php echo "
<table>
<tr>
<td></td>
</tr>
</table>
" ?>
Finally, slightly off-topic i know but another option for large chunks of text is to use the "here document" (here doc) format, this is a useful way to output text without having to worry about conflicting quotes. The string is defined by a unique identifier at the start and end of the string for example:
<?php
$text = <<<T1
Here is my text
spread across multiple lines
using the heredoc format.
T1; ?>
With "T1" in this case being the identifier, the only important points to remember are that the identifier in a heredoc string must use only alphanumeric characters or underscores, must start with an alphabetic character and there can be no spaces before the identifier in the last line.


