20

I've a larger piece of multi-line text that I need to put in an PHP associative array through a here-doc. It looks like this:

    $data = [
      "x" => "y",
      "foo" => "bar",
      /* ... other values ... */
      "idx" => <<< EOC
data data data data
data data data data
data data data data
EOC;
      "z" => 9,
      /* ... more values ... */
    ];

I can't figure out how to provide $data["idx"] the multi-line text with a heredoc.

miken32
  • 42,008
  • 16
  • 111
  • 154
bodacydo
  • 75,521
  • 93
  • 229
  • 319

2 Answers2

29

You can't end it with a semicolon at the end of the heredoc identifier. In PHP 7.2 and earlier the comma needs to be on a new line. It has to look like this:

$data = [
  "x" => "y",
  "foo" => "bar",
  /* ... other values ... */
  "idx" => <<<EOC
data data data data
data data data data
data data data data
EOC
 , "z" => 9,
 /* ... more values ... */
];
miken32
  • 42,008
  • 16
  • 111
  • 154
dan-lee
  • 14,365
  • 5
  • 52
  • 77
9

With PHP 7.3 things have improved significantly. You can now indent heredoc blocks:

$data = [
  "x" => "y",
  "foo" => "bar",
  /* ... other values ... */
  "idx" => <<<EOC
    data data data data
    data data data data
    data data data data
    EOC,
  "z" => 9,
  /* ... more values ... */
];
miken32
  • 42,008
  • 16
  • 111
  • 154
aross
  • 3,325
  • 3
  • 34
  • 42