1

I was going through Strings section in PHP manual (http://php.net/manual/en/language.types.string.php) but I was unable to figure out why create new heredoc or nowdoc string syntax when double quoted or single quoted string respectively do the same thing.

skull_king
  • 70
  • 8
  • 2
    See here http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php?rq=1 – Marc Giroux Jul 13 '16 at 13:16
  • Mostly because it's easier to locate a specifically defined end of string than a double quote when something spans over many lines and possibly contains tabs and all kinds of mess. In general I would tend to say: If you have strings spanning over multiple lines, consider extracting them into a file. They don't make code easy to read. – Till Helge Jul 13 '16 at 13:17
  • A plane and a train will both get you to your destination. Does that mean one is superfluous? – arkascha Jul 13 '16 at 13:20
  • @MarcGiroux: That link is difference between single and double quotes which i understand. My question is difference between Heredoc and Double quotes. Why create Heredoc syntax in first place when it does same thing as Double quotes. – skull_king Jul 13 '16 at 14:32
  • @arkascha : Plane has efficient running time than train. Do you mean hearedoc is efficient than double string or vice versa? – skull_king Jul 13 '16 at 14:33
  • Certainly a plane does _not_ necessarily have more efficiency, not at all! First that depends on distance and logistics, then it also actually depends on what you define as efficient. Time? Comfort? Power consumption? Ability to take heavy luggage or equipment? Ecological footprint? Different tools have different strong and weak sides. Having alternatives is a good thing. Sometimes you want to prefer `heredoc` or `nowdoc` since it is a _much_ better notation if it comes to readability of a string definition, for example when creating text templates. – arkascha Jul 13 '16 at 16:39

1 Answers1

2

The main difference is that you don't need to escape quotes in heredoc and howdoc. This can make large blocks of string easier to read. For example:

$html = "<p class=\"first\">$text</p>
         <p class=\"second\"$moretext</p>";

versus

    $html = <<<HTML
<p class="first">$text</p>
<p class="second">$moretext</p>
HTML;

From the docs:

Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped.

Matt S
  • 14,976
  • 6
  • 57
  • 76
  • Is this the only difference 'easier to read'? I feel even double quotes are not that difficult to read as they can also be spanned to multiuple lines. Escaping looks fine functionality else it will mean end of double quote. – skull_king Jul 13 '16 at 14:36
  • Yes, it's just a matter of personal preference. The only technical difference is the escaping of quotes. – Matt S Jul 13 '16 at 14:57
  • Actually the above example will give _different_ results. It is possible, but feels awkward to define exactly the same string using ordinary double quote definitions. – arkascha Jul 13 '16 at 16:42