Problems using Heredocs

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 $ , however it is giving this error:

Parse error: syntax error, unexpected end of file

If I did with quotation marks, it would work:

return
'value="<?php echo $questao'.$this->numQuestao.'">';
    
asked by anonymous 30.10.2015 / 02:55

2 answers

2

Heredocs error, according to official documentation , occurs when there are characters in the same line as the close handle

  

It is very important to verify that the   does not contain any other characters, except possibly   a semicolon (;). This means that the identifier can not   be indented, and that there can be no space or tabulations before   or after the semicolon.

So if you put EOT; to the page, it should work.

In your case, I think you do not need to use a heredocs, just do the string concatenation

<?php 

$campo = 'teste';

return "<input type='text' name='". $teste."'/>";
    
30.10.2015 / 11:25
1

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

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

So:

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

Output:

string(21) "minha string de
teste"

Remember that if you tab this string, the size of the tab will be considered.

Look again:

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

Output:

string(29) "    minha string de
    teste"

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

    
30.10.2015 / 16:17