21

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.

0

2 Answers 2

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 ... */
];
Sign up to request clarification or add additional context in comments.

1 Comment

Using [ "x" => "y"] instead of array("x" => "y") seems to be the best solution.
10

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 ... */
];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.