Is it possible to use Heredoc with special characters in an array?

Asked

Viewed 80 times

2

I have a multidimensional array, as the example below shows, but I wonder, if I can use a Heredoc on it, would it be possible? I could include special characters not escaped, if it is possible to use the Heredoc?

$name_code = array
  (
  array("Nome", "HEREDOC"),
  );

Code Heredoc which it intended to use in the array().

$script = <<<CODE
rand@#¨4key"'?></
CODE;

Below is my attempt, which failed.

$name_code = array
  (
  array("Nome", $script = <<<CODE
  rand@#¨4key"'?></
  CODE;),
  );
  • 1

    You tried and gave trouble?

  • Yes Parse error: syntax error, unexpected end of file in, I do not know if I was very clear, but I would like to user directly in the array, I added the question what I tried to do. I would like to have everything just in the array, and not out of it, leaving the code the most clean possible, facilitating upgrade/maintenance.

  • The way it is the code will not work anyway.. you must set the variable script outside the array, here it works: https://ideone.com/mKln9g

  • @zekk the biggest problem is in CODE;

  • 1

    @Florida care that in this case, the white spaces are part of the string.

2 answers

4


In principle, HEREDOC ends only when it finds the defined character sequence, at the beginning of the line and at most with ; at the end when necessary.

Correction of your case:

$name_code = array
  (
  array("Nome", <<<CODE
  rand@#¨4key"'?></
CODE
    ),
  );

See working on IDEONE.

Note: Your string has white spaces at the beginning   rand@#¨4key"'?></

Great care is to select a string that does not happen again.

If you do not want the characters to be escaped, and there is no substitution of variables, what you are looking for is NEWDOC, see the simple quotes:

$script = <<<'CODE'
rand@#¨4key"'?></
CODE;

Difference from HEREDOC to NOWDOC in PHP

Now, if you’re going to take the binary data from somewhere to "mount" PHP, you may have other problems with special characters reading the file. Then it would be necessary to use HEREDOC and escape the characters.

See a comparison on IDEONE.

  • Particularly, I find this syntax very messy. It "breaks" the idea of indentation.

  • For long HTML is a good, because indentation is separated from PHP

  • It’s a possible solution, it comes halfway out of my initial idea, but there are no other ways to do it all within the array? That’s the big question.

  • @Florida already updated. It was heredoc’s typo, I explained better.

  • In case, now I get the following error in CODE; Parse error: syntax error, unexpected ';', expecting ')'

  • 1

    Yes, it was a ; the more I pasted, these things have to read the error message :) - I updated and put a demo

  • Ah, yes, now you printed it perfectly, in fact I never knew that ; would not be necessary there.

Show 2 more comments

2

Perhaps not the solution you want, but another alternative to be considered would be the use of __halt_compiler combined with file_get_contents.

Behold:

$name_code = array (
  array(
   "Nome", file_get_contents(__FILE__, null, null, __COMPILER_HALT_OFFSET__)),
);

__halt_compiler();

Posso botar qualquer coisa aqui! $#<?php não vai ser processado ?>

It is possible to learn more about __halt_compiler in that great answer of the user @gmsantos.

  • If it doesn’t need another string, it’s a good alternative. But remember that this forces PHP to reopen the file as a stream

  • Verdade @Bacco.

  • I mean, it’s like creating a separate file and opening it. The value of __halt_compiler is more a convenience to leave everything in one place only to facilitate the organization. But it’s good you teach the guys :)

  • I’m using several strings, I just cleaned up the code to make it easier to understand.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.