Previous Page
Next Page

4.9. Here Documents

Use a heredoc when a multiline string exceeds two lines.

The "break-after-newlines-and-concatenate" approach is fine for a small number of lines, but it starts to become inefficientand uglyfor larger chunks of text.

For multiline strings that exceed two lines, use a heredoc:


    $usage = <<"END_USAGE";
    Usage: $0 <file> [-full] [-o] [-beans]
    Options:
        -full  : produce a full dump
        -o     : dump in octal
        -beans : source is Java
    END_USAGE

instead of:

    $usage = "Usage: $0 <file> [-full] [-o] [-beans]\n"
             . "Options:\n"
             . "    -full  : produce a full dump\n"
             . "    -o     : dump in octal\n"
             . "    -beans : source is Java\n"
             ;

    Previous Page
    Next Page