-2

why does this code block produce $text?

$text = 'This is text';

$text1 = <<<'TEXT'
$text
TEXT;

$text2 = <<<TEXT
$text1
TEXT;

echo "$text2"; 

The first heredoc(in $text1) is 'TEXT', with single quote, is it the same as the second heredoc(in $text2)?

PS. This question is about heredoc and nowdoc, not single and double quotes. It's not a dupliate

lamplanp
  • 55
  • 6
  • Nowdocs don't interpolate variables. See the [manual on strings](http://php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc). – mario Sep 01 '14 at 22:42
  • because you tell it to –  Sep 01 '14 at 22:42
  • The first one is [nowdoc](http://php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc). – raina77ow Sep 01 '14 at 22:42
  • This question is about heredoc and nowdoc, not single and double quotes. It's not a dupliate – lamplanp Sep 01 '14 at 22:49
  • @lamplanp: The accepted answer there explains heredoc and nowdoc as well. Besides that, you are asking for extremely basic stuff here that you should have already known about, had you only bothered to read up on it in the manual. – CBroe Sep 01 '14 at 23:11

2 Answers2

0

HEREDOC with single quotes (NOWDOC) does not parse the contents for any variables.

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.

http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Patrick Moore
  • 13,251
  • 5
  • 38
  • 63
0

The first one is a nowdoc, the second one is a heredoc. They are not the same.

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.

Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94