1

how can i embed php inside here doc style?

for example :

<?php
echo <<<EOF
<input type="radio" <?php echo "selected"; ?>>
EOF;
?>

I want to perform above task, suggest code for this or any alternative.

user47288
  • 40
  • 6
  • Why don't you just have the plain variable interpolated there? Heredoc strings operate mostly like double quoted strings. – mario Feb 07 '14 at 15:28
  • 2
    You can't put any code in it - that would arguably render the point of Heredoc moot. But you can use variables. [Use variable within heredoc in PHP (SQL practice)](http://stackoverflow.com/q/11274354) – Pekka Feb 07 '14 at 15:28

1 Answers1

3

The problem is that heredoc IS php code. Consider this example:

$sel = ' selected';
$var = <<<EOF
<input type="radio"${sel}>
EOF;
echo $var;

You'll notice that we don't need the < ?php open or ?> close tags to achieve this. This is because the interpreter parses and interprets all of this. You are over thinking it.

MORE EXPLAINATION: In the end, everything inside a heredoc statement needs to evaluate to a string. The way to accomplish what you want is to use php to create strings with everything in them BEFORE you get to the heredoc command. That way the heredoc command can do the only thing it knows how to do which is to create longer strings from shorter ones.

krowe
  • 2,129
  • 17
  • 19