Problems when using Heredocs

Asked

Viewed 107 times

0

return
    <<< EOT
        <input type="text" name="questao{$this->numQuestao}" value="<?php echo $questao{$this->numQuestao}?>"/>

EOT;

I needed to print this in my file html: <?php echo $questao{$this->numQuestao}?> with the $, but you’re making this mistake:

Parse error: syntax error, unexpected end of file

If I quote, it would work:

return
'value="<?php echo $questao'.$this->numQuestao.'">';
  • you want it to be "printable" the php code, right?

  • Insert double quotes at the beginning of the expression heredoc, to keep it that way : <<<'EOT'.

  • the closing tag EOT; nay may have no characters on the same line before, including spaces and tabs

2 answers

2

Error of the heredocs, according to the official documentation, occurs when a there are characters in the same row of the closing identifier

It is very important to verify that the line of the identifier of closure contains no other character, except possibly a semicolon (;). Meaning that the identifier cannot be indented, and that there can be no spaces or tabulations before or after the semicolon.

So if you join the EOT; left of page, should work.

In your case, I think you don’t need to use a heredocs, just perform string concatenation

<?php 

$campo = 'teste';

return "<input type='text' name='". $teste."'/>";

1

Your HEREDOC has a space after the <<<. Also another problem that occurs is to find that after the <<<EOT we can use spaces.

Generally, after the declaration <<<EOT comes a line break, the string being written just below.

Thus:

    $x = <<<EOT
minha string de
teste
EOT;
   var_dump($x);

Exit:

string(21) "minha string de
teste"

Remembering that if you stabilize this string, the tab size will be considered.

See again:

    $x = <<<EOT
    minha string de
    teste
EOT;
    var_dump($x);

Exit:

string(29) "    minha string de
    teste"

Note that the EOT; at the end, can not be tabilized. It has can not have spaces before.

Browser other questions tagged

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