echo "<input name=\"".$someName."\" type=\"input\" value=\"".$someValue."\" />\n";
It is quite easy to lose a double quote somewhere and spend quite a bit of time looking for it or matching pairs. The obvious alternative is to encase the PHP string in single quotes but this affects the PHP variable substitution mechanism which then requires further workarounds. Many people believe that (X)HTML or XML requires double quotes, but the W3C standards are quite clear - either " or ' can be used.
The solution is therefore to enclose strings in double quotes and place HTML attributes in single quotes. This allows the PHP substitution mechanism to function whilst allowing HTML to have its attributes enclosed as required by the standards. Our awkward statement shown above now looks like this
echo "<input name='$someName' type='input' value='$someValue' />\n";
This clearly eliminates a number of sources of confusion by removing the need to escape double quotes, removes the need to use concatenation characters and makes it much easier to match HTML (single) quotes up and it allows variable substitution to function as well. Of course, a problem occurs if you are using javascript in the HTML and the javascript requires parameters to be passed. In this situation the escaped double quotes are back, but their effect is minimal compared to the chaos that can ensue if everything was double quoted. So to extend our example a little further
echo "<input name='$someName' type='input' value='$someValue' onmouseup='javascript:a(\"
It is worth trying to keep your code as simple and clear as possible. Remember - if it goes wrong it will probably be you that has to edit it, so make your life as easy as possible.
They work in very similar ways.
By far the easiest way to show you is by example.
<?php
// Heredoc
echo <<< END_SOME_LABEL
<input name="{$someName}" type="input" value="{$someValue}" />
END_SOME_LABEL;
?>
Assuming that the variables have some values then this will output ...
<input name="richard" type="input" value="quadling" />
<?php
// 'Nowdoc'
echo <<< 'END_SOME_LABEL'
<input name="{$someName}" type="input" value="{$someValue}" />
'END_SOME_LABEL';
?>
Again, assuming that the variables have some values then this will output ...
<input name="{$someName}" type="input" value="{$someValue}" />
Yep. Nowdoc outputs the text as is.
You can also use a doublequoted label for heredoc ...
<?php
// "Heredoc"
echo <<< "END_SOME_LABEL"
<input name="{$someName}" type="input" value="{$someValue}" />
"END_SOME_LABEL";
?>
More can be read at http://docs.php.net/manual