/usr/portage

NOWDOC + double quotes = HEREDOC 9

PHP 5.3 introduces a new syntax element, NOWDOC. If you know HEREDOC, NOWDOC is easy to understand: it is in fact HEREDOC taken literally. Whily variables are expanded in HEREDOC, in NOWDOC they are not. Just to remind us, a small HEREDOC example:

$value = "Hello World!";
$var = <<<LABEL
$value
LABEL;

$var will contain “Hello World!” now.

<?php
$value = "Hello World!";
$var = <<<'LABEL'
$value
LABEL;

$value is not expanded, so $var contains literally “$value”.

For consistency and the sake of completeness, an alternative syntax has been introduced:

<?php
$value = "Hello World!";
$var = <<<"LABEL"
$value
LABEL;

Guess how it behaves …

Filed on 12-04-2008, 18:06 under & nine comments & no trackbacks

Trackbacks

Trackback specific URI for this entry

No Trackbacks

Comments

  1. Richard Heyes answers:
    published on April 12th 2008, 09:05:30 pm *

    So it’s comparable to single vs double quotes wrt variable expansion?

    BTW, your colours aren’t very good (from a low visibility point of view). In fact I’d go as far to say that they’re awful.

    Reply

  2. Lars Strojny returns:
    published on April 12th 2008, 09:31:20 pm *

    Exactly. The consistency argument applies because of double/single-quotes notation in variable assignments.

    Colors: you mean the syntax highlighting or the colors in general? If you refer to the syntax highlighting, this must be improved in fact. The rest seems to be fine, even for color blind people.

    Reply

  3. Richard Heyes responses:
    published on April 13th 2008, 01:05:54 pm *

    > Colors: you mean the syntax highlighting or the
    > colors in general? If you refer to the syntax
    > highlighting, this must be improved in fact. The
    > rest seems to be fine, even for color blind people.

    The colours in general. Speaking as someone with shit sight (I believe that’s a technical term), they aren’t. Not enough contrast by far. And this text area which is the same colour as your background – what’s the story with that? Are you trying to disguise it? :-)

    If you’re interested I wrote an paper on low visibility guidlines (which actually turned into general tips on improving usability for disabled users) here: http://www.phpguru.org/article.php/128

    At the very least I would advise providing a different stylesheet.

    Reply

  4. Philip Olson opines:
    published on April 12th 2008, 11:01:40 pm *

    Great new feature, thanks Gwynne for NOWDOC and thanks Lars for introducing the double quoted version.

    Next on the agenda, remove the requirement of forcing LABEL; to be flush left… ;-)

    Reply

  5. Lars Strojny supposes:
    published on April 14th 2008, 08:29:08 pm *

    Allowing the label to be in an abitrary location seems not to hard. What advantage would that have for you?

    Reply

  6. metapundit says:
    published on April 15th 2008, 07:19:43 pm *

    Next on the agenda, remove the requirement of forcing LABEL; to be flush left… ;-)

    Hear hear!

    It’s a tiny irritation, but the way the terminator works messes up auto-indenting and code folding features in my editor (by breaking the indentation levels). Even aside from my editor it’s just a visual irritant not to be able to indent code consistently.

    I’d also like to allow whitespace after the terminating label – I sometimes get an "unexpected end of file on line xxx" error that I stare at for a minute before realizing that a single (invisible) space after the semicolon is messing me up.

    Reply

  7. dave supposes:
    published on April 16th 2008, 03:26:33 pm *

    1. Thanks for your post about NOWDOC!
    2. I agree totally with Philip (and others) about the syntax of HEREDOC and NOWDOC. There should be more freedom in how the LABEL terminator is aligned (as well as use of spaces, etc.) with prior coding. Thanks.
    3. I’m also recently sight impaired and some additional contrast on your website would be helpful.

    Thank you again for this information.
    >>>Dave

    Reply

  8. Jason Priebe says:
    published on August 4th 2009, 03:49:08 pm *

    I’m a huge HEREDOC fan, so any new versatility is welcome.

    One thing I wish PHP had is something akin to the qq() function in perl. qq() lets you specify a string with any sort of quotes inline:

    $foo = qq (a href="..." onclick="alert(‘hello’);");

    No escaping of either single- or double-quotes! (I had to omit the brackets on my example HTML tag to get it through the comment filter).

    Reply

  9. Lars Strojny means:
    published on August 4th 2009, 03:59:45 pm *

    Sounds like a good feature to have in a language like PHP. I have to think a bit more, but the following should be unambigous enough to implement it easily:

    $foo = quote ... a href=&quot;foo&quot; onclick=&quot;alert('1')&quot;;
    

    Reply

Add a Comment & let me know what you think