Forum Moderators: coopster

Message Too Old, No Replies

Nowdoc syntax

Since PHP 5.3.0

         

eelixduppy

3:08 pm on Jun 12, 2008 (gmt 0)




Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping. It shares some features in common with the SGML <![CDATA[ ]]> construct, in that it declares a block of text which is not for parsing.

[php.net...]

Very cool. Not sure how much I'd use this, though, before I just closed my php tags and reopened them afterwards.

coopster

1:47 am on Jun 13, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I guess I'm trying to figure out the value as well and am left empty as far as thoughts go so far.

eelixduppy

3:56 am on Jun 13, 2008 (gmt 0)



The only time I can think of that it would be somewhat useful is if you had a variable that needed to contain a string with mixed quotes in it so that you wouldn't have to escape anything:

$var = <<<'HTML'
and she said "He's so blah blah" but that isn't blah blah
HTML;

Opposed to:


$var = 'and she said "He\'s so blah blah" but that isn\'t blah blah';
$var = "and she said \"He's so blah blah\" but that isn't blah blah";

PHP_Chimp

10:15 am on Jun 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$var_now = <<<'HTML'
and she said "He's so blah blah" but that isn't blah blah
HTML;
$var_here = <<<HTML
and she said "He's so blah blah" but that isn't blah blah
HTML;
echo $var_now;
echo $var_here;

Give you the same thing. So nothing new from heredoc.

The only use I have found so far is for declaring class variables.

<?php
class foo {
// As of PHP 5.3.0
public $bar = <<<'EOT'
bar
EOT;
}
?>

penders

1:21 pm on Jun 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The construct is ideal for embedding PHP code...

...if you want to store a large chunk of actual PHP code in a variable... echo it as part of a PHP tutorial and eval() it later to demo?